【发布时间】:2021-11-29 11:15:43
【问题描述】:
我的 Heroku Node.js REST api 有时会崩溃。通过日志,我看到了这个
2021-10-10T08:47:05.671873+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=test-restapi2.herokuapp.com request_id=0926fc20-8cb5-405f-8072-d3146f2c2b4c fwd="154.118.12.15" dyno= connect= service= status=503 bytes= protocol=https
2021-10-10T08:48:40.161508+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=test-restapi2.herokuapp.com request_id=1e36d609-b743-491a-9222-6d7d1e5ed0c4 fwd="154.118.12.15" dyno= connect= service= status=503 bytes= protocol=https
2021-10-10T08:49:30.471556+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-10T08:49:32.337007+00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-10T08:49:33.535533+00:00 app[web.1]:
2021-10-10T08:49:33.535549+00:00 app[web.1]: > restapi_test@1.0.0 start /app
2021-10-10T08:49:33.535549+00:00 app[web.1]: > node app.js
2021-10-10T08:49:33.535550+00:00 app[web.1]:
2021-10-10T08:49:33.693859+00:00 app[web.1]: App running
2021-10-10T08:49:34.109271+00:00 heroku[web.1]: State changed from starting to up
2021-10-10T08:50:15.944488+00:00 heroku[router]: at=info method=GET path="/" host=test-restapi2.herokuapp.com request_id=f737677f-27ce-4827-9056-943d6e621b4e fwd="154.118.12.15" dyno=web.1 connect=0ms service=8ms status=200 bytes=239 protocol=https
2021-10-10T08:50:24.414304+00:00 heroku[router]: at=info method=GET path="/employees" host=test-restapi2.herokuapp.com request_id=12b5e45a-bf88-4af8-99ac-72b21397aad7 fwd="154.118.12.15" dyno=web.1 connect=0ms service=8ms status=200 bytes=561 protocol=https
2021-10-10T08:51:23.663210+00:00 app[web.1]: events.js:377
2021-10-10T08:51:23.663235+00:00 app[web.1]: throw er; // Unhandled 'error' event
2021-10-10T08:51:23.663235+00:00 app[web.1]: ^
2021-10-10T08:51:23.663235+00:00 app[web.1]:
2021-10-10T08:51:23.663237+00:00 app[web.1]: Error: Connection lost: The server closed the connection.
2021-10-10T08:51:23.663237+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:112:13)
2021-10-10T08:51:23.663238+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28)
2021-10-10T08:51:23.663238+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
2021-10-10T08:51:23.663238+00:00 app[web.1]: at Socket.emit (events.js:412:35)
2021-10-10T08:51:23.663239+00:00 app[web.1]: at endReadableNT (internal/streams/readable.js:1334:12)
2021-10-10T08:51:23.663239+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:82:21)
2021-10-10T08:51:23.663239+00:00 app[web.1]: Emitted 'error' event on Connection instance at:
2021-10-10T08:51:23.663240+00:00 app[web.1]: at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8)
2021-10-10T08:51:23.663240+00:00 app[web.1]: at Protocol.emit (events.js:400:28)
2021-10-10T08:51:23.663241+00:00 app[web.1]: at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
2021-10-10T08:51:23.663241+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:116:8)
2021-10-10T08:51:23.663241+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28)
2021-10-10T08:51:23.663242+00:00 app[web.1]: [... lines matching original stack trace ...]
2021-10-10T08:51:23.663242+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:82:21) {
2021-10-10T08:51:23.663242+00:00 app[web.1]: fatal: true,
2021-10-10T08:51:23.663242+00:00 app[web.1]: code: 'PROTOCOL_CONNECTION_LOST'
2021-10-10T08:51:23.663243+00:00 app[web.1]: }
2021-10-10T08:51:23.670839+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-10T08:51:23.671001+00:00 app[web.1]: npm ERR! errno 1
2021-10-10T08:51:23.673771+00:00 app[web.1]: npm ERR! restapi_test@1.0.0 start: `node app.js`
2021-10-10T08:51:23.673814+00:00 app[web.1]: npm ERR! Exit status 1
2021-10-10T08:51:23.673870+00:00 app[web.1]: npm ERR!
2021-10-10T08:51:23.673906+00:00 app[web.1]: npm ERR! Failed at the restapi_test@1.0.0 start script.
2021-10-10T08:51:23.673944+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-10T08:51:23.676898+00:00 app[web.1]:
2021-10-10T08:51:23.676985+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-10T08:51:23.677026+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-10-10T08_51_23_674Z-debug.log
2021-10-10T08:51:23.808417+00:00 heroku[web.1]: Process exited with status 1
2021-10-10T08:51:23.874053+00:00 heroku[web.1]: State changed from up to crashed
2021-10-10T08:51:23.878330+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-10T08:51:26.109466+00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-10T08:51:27.678219+00:00 app[web.1]:
2021-10-10T08:51:27.678234+00:00 app[web.1]: > restapi_test@1.0.0 start /app
2021-10-10T08:51:27.678234+00:00 app[web.1]: > node app.js
2021-10-10T08:51:27.678235+00:00 app[web.1]:
2021-10-10T08:51:27.952382+00:00 app[web.1]: App running
2021-10-10T08:51:28.210981+00:00 heroku[web.1]: State changed from starting to up
现在我很困惑,因为我对几个主题进行了分类,但我一直无法解决挑战是什么。 我的测试 Node.js REST api 代码看起来像这样
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));
app.get('/',function(req , res){
return res.send({error: true,message: 'hello'})
});
var dbConn = mysql.createConnection({
host: '**********',
user: '*************',
password: '*******',
database: '**********'
});
dbConn.connect();
app.get('/employees',function(req,res){
dbConn.query('SELECT * FROM employeedb',function(error, results, fields){
if (error) throw error;
return res.send({ error: false, data: results, message: 'users list' });
});
});
app.listen(process.env.PORT, '0.0.0.0',function(){
console.log('App running');
});
module.exports = app;
Package.json 看起来像这样
{
"name": "restapi_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mysql": "^2.18.1"
}
}
如果我做的不对,请提出建议。
【问题讨论】: