标准Express接口
Server.js代码:
// Pull in required dependencies
var express = require('express');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var port = process.env.PORT || 3000;
var app = express();
// Serve static content for the app from the 'public' directory
app.use(express.static(process.cwd() + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
// Override with POST having ?_method=DELETE
app.use(methodOverride('_method'));
// Set Handlebars as the view engine
var exphbs = require('express-handlebars');
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
// Import routes and give the server access to them
var routes = require('./controllers/burgers_controller.js');
app.use('/', routes);
app.listen(port);
连接查找JAWSdb插件首先,如果没有连接到本地MySQL。
Connection.js代码:
// Pull in required dependencies
var mysql = require('mysql');
// Create the MySQL connection object
var connection;
if (process.env.JAWSDB_URL) {
// DB is JawsDB on Heroku
connection = mysql.createConnection(process.env.JAWSDB_URL);
} else {
// DB is local on localhost
connection = mysql.createConnection({
port: 3306,
host: 'localhost',
user: 'test',
password: 'test123',
database: 'burgers_db'
})
};
// Make the connection to MySQL
connection.connect(function (err) {
if (err) {
console.error('ERROR: MySQL connection error -- ' + err.stack + '\n\n');
return;
}
console.log('Connected to MySQL database as id ' + connection.threadId + '\n\n');
});
// Export connection for ORM use
module.exports = connection;
我不认为我的思念和依赖...
的package.json代码:
{
"name": "eat-da-burger",
"version": "1.0.0",
"description": "Buger eating application with Node.js, Express, Handlebars, and MySQL.",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Hussein Fakhreddine",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"express-handlebars": "^3.0.0",
"method-override": "^2.3.7",
"mysql": "^2.13.0"
}
}
我得到一个错误说没有这样的表在日志中。我不知道这意味着什么?
Heroku的日志:
2018-02-24T23:24:22.364961+00:00 app[web.1]:
2018-02-24T23:24:22.653768+00:00 heroku[web.1]: State changed from starting to up
2018-02-24T23:24:36.540291+00:00 app[web.1]: /app/node_modules/mysql/lib/protocol/Parser.js:80
2018-02-24T23:24:36.540306+00:00 app[web.1]: throw err; // Rethrow non-MySQL errors
2018-02-24T23:24:36.540307+00:00 app[web.1]: ^
2018-02-24T23:24:36.540309+00:00 app[web.1]:
2018-02-24T23:24:36.540310+00:00 app[web.1]: Error: ER_NO_SUCH_TABLE: Table 'blmzyk0sphz3yq4s.burgers' doesn't exist
2018-02-24T23:24:36.540312+00:00 app[web.1]: at Query.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
2018-02-24T23:24:36.540314+00:00 app[web.1]: at Query.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
2018-02-24T23:24:36.540315+00:00 app[web.1]: at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:279:23)
2018-02-24T23:24:36.540316+00:00 app[web.1]: at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:76:12)
2018-02-24T23:24:36.540318+00:00 app[web.1]: at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:39:16)
2018-02-24T23:24:36.540319+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:103:28)
2018-02-24T23:24:36.540320+00:00 app[web.1]: at emitOne (events.js:116:13)
2018-02-24T23:24:36.540321+00:00 app[web.1]: at Socket.emit (events.js:211:7)
2018-02-24T23:24:36.540323+00:00 app[web.1]: at addChunk (_stream_readable.js:263:12)
2018-02-24T23:24:36.540324+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:250:11)
2018-02-24T23:24:36.540326+00:00 app[web.1]: --------------------
2018-02-24T23:24:36.540327+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2018-02-24T23:24:36.540328+00:00 app[web.1]: at Connection.query (/app/node_modules/mysql/lib/Connection.js:208:25)
2018-02-24T23:24:36.540329+00:00 app[web.1]: at Object.selectAll (/app/config/orm.js:34:14)
2018-02-24T23:24:36.540330+00:00 app[web.1]: at Object.selectAll (/app/models/burger.js:8:9)
2018-02-24T23:24:36.540331+00:00 app[web.1]: at /app/controllers/burgers_controller.js:10:10
2018-02-24T23:24:36.540333+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2018-02-24T23:24:36.540334+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2018-02-24T23:24:36.540335+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2018-02-24T23:24:36.540336+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2018-02-24T23:24:36.540337+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2018-02-24T23:24:36.547596+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-02-24T23:24:36.547898+00:00 app[web.1]: npm ERR! errno 1
2018-02-24T23:24:36.549079+00:00 app[web.1]: npm ERR! [email protected] start: `node server.js`
2018-02-24T23:24:36.549202+00:00 app[web.1]: npm ERR! Exit status 1
2018-02-24T23:24:36.549395+00:00 app[web.1]: npm ERR!
2018-02-24T23:24:36.549506+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2018-02-24T23:24:36.549631+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-02-24T23:24:36.559783+00:00 app[web.1]:
2018-02-24T23:24:36.559932+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-02-24T23:24:36.560010+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2018-02-24T23_24_36_555Z-debug.log
2018-02-24T23:24:36.544178+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=burgerorder.herokuapp.com request_id=1ffcc0a3-fe15-49a7-80bf-dae378355c0d fwd="172.90.83.64" dyno=web.1 connect=2ms service=18ms status=503 bytes=0 protocol=https
2018-02-24T23:24:36.616665+00:00 heroku[web.1]: State changed from up to crashed
2018-02-24T23:24:36.619541+00:00 heroku[web.1]: State changed from crashed to starting
2018-02-24T23:24:36.604174+00:00 heroku[web.1]: Process exited with status 1
2018-02-24T23:24:39.573704+00:00 heroku[web.1]: Starting process with command `npm start`
2018-02-24T23:24:43.233438+00:00 app[web.1]: Connected to MySQL database as id 48180
2018-02-24T23:24:43.233475+00:00 app[web.1]:
2018-02-24T23:24:43.233478+00:00 app[web.1]:
2018-02-24T23:24:42.532808+00:00 app[web.1]:
2018-02-24T23:24:42.532838+00:00 app[web.1]: > [email protected] start /app
2018-02-24T23:24:42.532845+00:00 app[web.1]: > node server.js
2018-02-24T23:24:42.532846+00:00 app[web.1]:
2018-02-24T23:24:43.889656+00:00 heroku[web.1]: State changed from starting to up
2018-02-24T23:24:45.147650+00:00 app[web.1]: /app/node_modules/mysql/lib/protocol/Parser.js:80
2018-02-24T23:24:45.147675+00:00 app[web.1]: throw err; // Rethrow non-MySQL errors
2018-02-24T23:24:45.147678+00:00 app[web.1]: ^
2018-02-24T23:24:45.147681+00:00 app[web.1]: Error: ER_NO_SUCH_TABLE: Table 'blmzyk0sphz3yq4s.burgers' doesn't exist
2018-02-24T23:24:45.147679+00:00 app[web.1]:
2018-02-24T23:24:45.147684+00:00 app[web.1]: at Query.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
2018-02-24T23:24:45.147686+00:00 app[web.1]: at Query.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
2018-02-24T23:24:45.147687+00:00 app[web.1]: at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:279:23)
2018-02-24T23:24:45.147689+00:00 app[web.1]: at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:76:12)
2018-02-24T23:24:45.147691+00:00 app[web.1]: at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:39:16)
2018-02-24T23:24:45.147692+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:103:28)
2018-02-24T23:24:45.147694+00:00 app[web.1]: at emitOne (events.js:116:13)
2018-02-24T23:24:45.147697+00:00 app[web.1]: at Socket.emit (events.js:211:7)
2018-02-24T23:24:45.147699+00:00 app[web.1]: at addChunk (_stream_readable.js:263:12)
2018-02-24T23:24:45.147701+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:250:11)
2018-02-24T23:24:45.147703+00:00 app[web.1]: --------------------
2018-02-24T23:24:45.147704+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2018-02-24T23:24:45.147706+00:00 app[web.1]: at Connection.query (/app/node_modules/mysql/lib/Connection.js:208:25)
2018-02-24T23:24:45.147708+00:00 app[web.1]: at Object.selectAll (/app/config/orm.js:34:14)
2018-02-24T23:24:45.147710+00:00 app[web.1]: at Object.selectAll (/app/models/burger.js:8:9)
2018-02-24T23:24:45.147711+00:00 app[web.1]: at /app/controllers/burgers_controller.js:10:10
2018-02-24T23:24:45.147713+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2018-02-24T23:24:45.147715+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2018-02-24T23:24:45.147717+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2018-02-24T23:24:45.147718+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2018-02-24T23:24:45.147720+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2018-02-24T23:24:45.162655+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-02-24T23:24:45.163528+00:00 app[web.1]: npm ERR! errno 1
2018-02-24T23:24:45.166236+00:00 app[web.1]: npm ERR! [email protected] start: `node server.js`
2018-02-24T23:24:45.166533+00:00 app[web.1]: npm ERR! Exit status 1
2018-02-24T23:24:45.167098+00:00 app[web.1]: npm ERR!
2018-02-24T23:24:45.167408+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2018-02-24T23:24:45.167693+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-02-24T23:24:45.183200+00:00 app[web.1]:
2018-02-24T23:24:45.183858+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-02-24T23:24:45.184053+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2018-02-24T23_24_45_176Z-debug.log
2018-02-24T23:24:45.156737+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=burgerorder.herokuapp.com request_id=7ba6ffb8-2ba8-45f9-9325-f48155fad950 fwd="172.90.83.64" dyno=web.1 connect=1ms service=50ms status=503 bytes=0 protocol=https
2018-02-24T23:24:45.280176+00:00 heroku[web.1]: State changed from up to crashed
2018-02-24T23:24:45.261501+00:00 heroku[web.1]: Process exited with status 1
2018-02-24T23:25:33.215059+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=burgerorder.herokuapp.com request_id=ed4e8e33-a27d-42ae-8797-82918277154d fwd="172.90.83.64" dyno= connect= service= status=503 bytes= protocol=https
2018-02-24T23:37:19.469262+00:00 heroku[web.1]: State changed from crashed to starting
2018-02-24T23:37:21.346733+00:00 heroku[web.1]: Starting process with command `npm start`
2018-02-24T23:37:22.947106+00:00 app[web.1]:
2018-02-24T23:37:22.947123+00:00 app[web.1]: > [email protected] start /app
2018-02-24T23:37:22.947125+00:00 app[web.1]: > node server.js
2018-02-24T23:37:22.947127+00:00 app[web.1]:
2018-02-24T23:37:23.298062+00:00 app[web.1]: Connected to MySQL database as id 51410
2018-02-24T23:37:23.298091+00:00 app[web.1]:
2018-02-24T23:37:23.298093+00:00 app[web.1]:
2018-02-24T23:37:23.569144+00:00 heroku[web.1]: State changed from starting to up
添加burgers_controllers.js代码:
// Pull in required dependencies
var express = require('express');
var router = express.Router();
// Import the model (burger.js) to use its database functions.
var burger = require('../models/burger.js');
// Create the routes and associated logic
router.get('/', function(req, res) {
burger.selectAll(function(data) {
var hbsObject = {
burgers: data
};
// console.log(hbsObject);
res.render('index', hbsObject);
});
});
router.post('/burgers', function(req, res) {
burger.insertOne([
'burger_name'
], [
req.body.burger_name
], function(data) {
res.redirect('/');
});
});
router.put('/burgers/:id', function(req, res) {
var condition = 'id = ' + req.params.id;
burger.updateOne({
devoured: true
}, condition, function(data) {
res.redirect('/');
});
});
// Export routes for server.js to use.
module.exports = router;
-- Create the burgers_db database --
CREATE DATABASE
IF NOT EXISTS burgers_db;
USE burgers_db;
-- Create a burgers table with the required fields --
CREATE TABLE burgers
(
id int NOT NULL
AUTO_INCREMENT,
burger_name varchar
(255) NOT NULL,
devoured BOOLEAN DEFAULT false,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON
UPDATE CURRENT_TIMESTAMP,
dt DATETIME
DEFAULT CURRENT_TIMESTAMP ON
UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY
(id)
);
orm.js代码:
// Import the MySQL connection object
var connection = require ('./connection.js');
// Helper function for generating MySQL syntax
function printQuestionMarks(num) {
var arr = [];
for (var i = 0; i < num; i++) {
arr.push("?");
}
return arr.toString();
}
// Helper function for generating My SQL syntax
function objToSql(ob) {
var arr = [];
for (var key in ob) {
arr.push(key + "=" + ob[key]);
}
return arr.toString();
}
// Create the ORM object to perform SQL queries
var orm = {
// Function that returns all table entries
selectAll: function(tableInput, cb) {
// Construct the query string that returns all rows from the target table
var queryString = "SELECT * FROM " + tableInput + ";";
// Perform the database query
connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
// Return results in callback
cb(result);
});
},
// Function that insert a single table entry
insertOne: function(table, cols, vals, cb) {
// Construct the query string that inserts a single row into the target table
var queryString = "INSERT INTO " + table;
queryString += " (";
queryString += cols.toString();
queryString += ") ";
queryString += "VALUES (";
queryString += printQuestionMarks(vals.length);
queryString += ") ";
// console.log(queryString);
// Perform the database query
connection.query(queryString, vals, function(err, result) {
if (err) {
throw err;
}
// Return results in callback
cb(result);
});
},
// Function that updates a single table entry
updateOne: function(table, objColVals, condition, cb) {
// Construct the query string that updates a single entry in the target table
var queryString = "UPDATE " + table;
queryString += " SET ";
queryString += objToSql(objColVals);
queryString += " WHERE ";
queryString += condition;
// console.log(queryString);
// Perform the database query
connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
// Return results in callback
cb(result);
});
}
};
// Export the orm object for use in other modules
module.exports = orm;
burger.js代码:
// Import the ORM to implement functions that will interact with the database
var orm = require('../config/orm.js');
// Create the burger object
var burger = {
// Select all burger table entries
selectAll: function(cb) {
orm.selectAll('burgers', function(res) {
cb(res);
});
},
// The variables cols and vals are arrays
insertOne: function(cols, vals, cb) {
orm.insertOne('burgers', cols, vals, function(res) {
cb(res);
});
},
// The objColVals is an object specifying columns as object keys with associated values
updateOne: function(objColVals, condition, cb) {
orm.updateOne('burgers', objColVals, condition, function(res) {
cb(res);
});
}
};
// Export the database functions for the controller (burgerController.js).
module.exports = burger;
当我在节点上运行的服务器就好了它的工作原理。但是,当推到Heroku的掌握应用甚至不推成功后的工作。我缺少的东西吗?
您正在使用在Heroku JAWS_DB,所以你必须安装玛丽亚JawsDB插件,然后连接到JawsDB有存储,而不是依赖于你的本地计算机上创建的表你的表/模式...