我有以下连接到SQL db的nodejs应用程序。一旦调用API,它就会成功但应用程序崩溃。我是nodejs的新手,可以准确理解这个问题。
我从rest客户端工具执行了这个帖子。它取得了成功,但之后app崩溃了。带有必需参数的https://localhost:8089/updateUserBalance。
var http = require('http');
var path = require('path');
var express = require('express');
var mysql = require('mysql');
var bodyParser = require('body-parser');
var db = mysql.createConnection({
host: 'xxxxxxxxxx',
port: '3306',
user: 'xxxxxxxxxx',
password: '394e1836',
database: 'ad_fd0a197d2dcabf7'
});
var port = 8089;//(process.env.VCAP_APP_PORT || 3000);
var host = 'localhost';//(process.env.VCAP_APP_HOST || 'localhost');
app.set('port', port);
app.use(express.logger());
app.use(express.bodyParser());
// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse the raw data
app.use(bodyParser.raw());
// parse text
app.use(bodyParser.text());
var jsonParser = bodyParser.json();
//var jsonParser = bodyParser.raw();
var productsRouter=express.Router();app.use(app.router);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
function handle_database(req,res) {
pool.getConnection(function(err,connection){
if (err) {
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
connection.on('error', function(err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
app.post('/updateUserBalance',function(req, res, next){
setSafeUpdate();
//ToDo update timestamp also
var sql = "update USERDETAILS set balance ='"+req.body.balance+"'where username ='"+req.body.username+"';";
var query = db.query(sql, function(err, result) {
if (err) {
console.error(err);
return res.send(err);
} else {
return res.send('Ok');
}
});
});
app.post('/getUserDetails', function(req, res, next){
var sql = "select * from USERDETAILS where username ='"+req.body.username+"'order by timestamp limit 1;";
console.log(sql);
var query = db.query(sql,function(err, result) {
if (err) {
console.error(err);
return res.send(err);
} else {
res.json({ "Message" : "Success", "Record" : result});
}
});
});
// start server
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening at http://' + host + ':' + port);
});
这是崩溃日志
C:\\app>node app.js
Express server listening at http://localhost:8089
safeupdate : [object Object]
::1 - - [Thu, 15 Dec 2016 08:33:55 GMT] "POST /updateUserBalance HTTP/1.1" 200
"-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko
Chrome/54.0.2840.99 Safari/537.36"
events.js:160
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (C:\bluemix-node-mysql-uploader-master\app\node_modules\mys
l\lib\protocol\Protocol.js:109:13)
at Socket.<anonymous> (C:\bluemix-node-mysql-uploader-master\app\node_modul
s\mysql\lib\Connection.js:115:28)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)