我想使用node.js查询mySQL数据库,并将结果作为JSON返回,以便在移动应用程序中使用。不幸的是,我的请求只是超时了,服务器在整整2分钟内什么都不做,直到日志文件显示我的console.log()
-语句。
此外,回调不会返回任何结果。它只是空的。
// Check dependencies
var http = require('http');
// Create the http server.
// reference: http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/
http.createServer(function(request, response) {
// Attach listener on end event.
request.on('close', function() {
console.log('request');
// run asynchronous
getSQL(function(err, result) {
console.log('json:', result);
response.writeHead(200, {
'Content-Type' : 'x-application/json'
});
// Send data as JSON string.
response.end(result);
});
});
}).listen(3000);
// Access MySQL via node-mysql
// https://github.com/felixge/node-mysql
function getSQL(callback) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'pw',
database : 'db',
socketPath : '/var/run/mysqld/mysqld.sock', // socket for communication from debian <-> client, seems not to be set correcly by default?
});
connection.connect();
var json = '';
var query = 'SELECT * FROM test';
connection.query(query, function(err, results, fields) {
if (err)
return callback(err, null);
console.log('The result is: ', results[0]);
// wrap result-set as json
json = JSON.stringify(results);
});
connection.end();
callback(null, json);
};
大约2分钟后输出:
$ node app.js
request
json:
The result is: { test: 'avc' }
json2: [{"test":"avc"}]
基于我对整个node.js概念的基本理解,我的代码应该查询数据库(它确实查询了),并在完成后通过回调函数返回一个json(显然没有),然后将其作为响应发送回客户端(由于json是空的,所以无法真正检查)。
我想我犯了一个(或几个)重大错误。非常感谢帮助和/或有用的链接。谢谢
溶液,得益于六氰
// Check dependencies
var http = require('http');
// Create the http server.
// reference: http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/
/***************
* Correction 1: Using the request.on('close', function()( ... )-listener isn't required anymore
***************/
http.createServer(function(req, res) {
console.log('Receving request...');
var callback = function(err, result) {
res.writeHead(200, {
'Content-Type' : 'x-application/json'
});
console.log('json:', result);
res.end(result);
};
getSQL(callback);
}).listen(3000);
// Access MySQL via node-mysql
// https://github.com/felixge/node-mysql
function getSQL(callback) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'pw',
database : 'db',
socketPath : '/var/run/mysqld/mysqld.sock', // socket for communication from debian <-> client, seems not to be set correcly by default?
});
connection.connect();
var json = '';
var query = 'SELECT * FROM test';
connection.query(query, function(err, results, fields) {
if (err)
return callback(err, null);
console.log('The query-result is: ', results[0]);
// wrap result-set as json
json = JSON.stringify(results);
/***************
* Correction 2: Nest the callback correctly!
***************/
connection.end();
console.log('JSON-result:', json);
callback(null, json);
});
};
您遵循的是一个较旧的指南,该指南指示您在发送响应之前等待请求的close
事件,但实际上您不再需要这样做。
现在的情况是你没有发送回复,所以你的客户超时了。只有当客户端超时时,才会触发close
事件。由于客户端在您发送响应时已断开连接,因此您在客户端上不会得到任何信息,只能在终端中看到。
要解决这个问题,只需停止等待关闭事件,并在调用请求处理程序时立即运行代码:
var http = require('http');
http.createServer(function(req, res) {
getSQL(function(err, result) {
res.writeHead(200, {
'Content-Type' : 'x-application/json'
});
res.end(result);
});
}).listen(3000);