我正在开发一个 Node.js 应用程序(在 Heroku 中允许),它会生成 response.write() 并写入 JSON 文本。使用hurl.it工具,我可以测试这些 http 请求,但我不明白为什么在同一个 http 请求中,有时会出现第一个标头或有时会出现另一个标头。
我只想要第一个。我怎么能总是有第一个而从来没有第二个?
HEADERS
Connection: Close
Content-Length: 877
Content-Type: application/json
Date: Wed, 26 Mar 2014 00:25:06 GMT
或者有时,
HEADERS
Connection: Close
Content-Type: application/json
Date: Wed, 26 Mar 2014 00:27:22 GMT
Transfer-Encoding: chunked
正如你所看到的,我不知道它可能是什么,所以我不知道哪些信息可以用来分享。所以如果你有任何想法,请分享!
JSON 来自使用 mysql npm 的 SQL 查询。就像是:
var mysql = require('mysql');
var db_config = {...};
var connection;
function handleDisconnect() {
console.log('1. connecting to db:');
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if (err) { // or restarting (takes a while sometimes).
console.log('2. error when connecting to db:', err);
setTimeout(handleDisconnect, 1000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('3. db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
console.log(id);
connection.query("select * from table where id = '"+id+"'", function(err, rows, fields){
if (err){
console.log(err);
throw err;
}
var objToJson = rows;
objToJson.response = response;
var finalresponse = JSON.stringify(objToJson);
response.write('{ "firstdata": ')
response.write(finalresponse);
var jsonResult = JSON.parse(finalresponse);
var ordersplit = jsonResult[0].order_split
if(ordersplit == 0){
connection.query("Call getUnit('"+id+"');", function(err, rows, fields){
if (err){
console.log(err);
throw err;
}
var objToJson = rows[0];
objToJson.response = response;
response.write(', "data": ');
response.write(JSON.stringify(objToJson));
response.write('}');
response.end();
});
}
if(ordersplit == 1){
connection.query("Call getUnitCustomize('"+id+"');", function(err, rows, fields){
if (err){
console.log(err);
throw err;
}
var objToJson = rows[0];
objToJson.response = response;
response.write(', "data": ');
response.write(JSON.stringify(objToJson));
response.write('}');
response.end();
});
}
connection.end();
});