我试图将我的项目推送到heroku并将jawsdb与应用程序连接起来。我将jawsdb信息输入到我的server.js并更新它然后推送到heroku。但我得到这个erorr并且应用程序不会加载。我认为这与我设置数据库的方式有关。
我收到以下“拒绝用户访问”错误:
这是我的代码:server.js
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var logger = require('morgan');
var request = require('request');
var cheerio = require('cheerio');
var methodOverride = require('method-override')
var fs = require("fs");
var hbs = require('hbs');
// Set up Express
var app = express();
var router = express.Router();
//handlebars
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
// override with POST having ?_method=DELETE
app.use(methodOverride('_method'))
// Set up Mysql
var con = mysql.createConnection({
host: "t89yihg12rw77y6f.cbetxkdyhwsb.us-east-1.rds.amazonaws.com",
port: 3306,
user: "swvr0i1j9ny720mk",
password: "e3lzkqag4dmeqhup"
});
//conecting to mysql
con.connect(function(err) {
if (err) throw err;
console.log("Database connected to the matrix..");
});
con.query('CREATE DATABASE IF NOT EXISTS warehouse', function (err) {
if (err) throw err;
con.query('USE warehouse', function (err) {
if (err) throw err;
con.query('CREATE TABLE IF NOT EXISTS storage('
+ 'id INT NOT NULL AUTO_INCREMENT,'
+ 'PRIMARY KEY(id),'
+ 'link VARCHAR(255),'
+ 'item VARCHAR(255),'
+ 'stock VARCHAR(255)'
+ ')', function (err) {
if (err) throw err;
});
});
});
// Parse application/x-www-form-urlencoded
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Serve static content for the app from the "public" directory in the application directory.
app.use(express.static(__dirname + '/public'));
//prints database to page
app.get('/index', function(req,res) {
con.query('SELECT * FROM storage;', function(err, data) {
if (err) throw err;
//test it
//console.log('The solution is: ', data);
//test it
//res.send(data);
res.render('index', {storage : data});
});
});
//delete data entry
app.delete('/delete', function(req,res){
con.query('DELETE FROM storage WHERE id = ?', [req.body.id], function(err, result) {
if (err) throw err;
res.redirect('/index');
});
});
// Open Server
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
JawsDB不允许您选择数据库名称或创建新的数据库名称。相反,您需要通过工作台工具进行连接,并查看他们给您的数据库名称(例如,下面显示我的数据库名称JawsDB已分配)。
然后,在您的代码中使用此数据库,而不是您命名为“仓库”的数据库。例如,如果我使用为我命名的模式JawsDB来执行此操作,我将使用'jdjkhrjps1cgj89h'
con.query('USE jdjkhrjps1cgj89h', function (err) {
if (err) throw err;
con.query('CREATE TABLE IF NOT EXISTS storage('
+ 'id INT NOT NULL AUTO_INCREMENT,'
+ 'PRIMARY KEY(id),'
+ 'link VARCHAR(255),'
+ 'item VARCHAR(255),'
+ 'stock VARCHAR(255)'
+ ')', function (err) {
if (err) throw err;
});
});