相同的HTTP请求,不同的HTTP标头

时间:2014-03-26 00:35:56

标签: node.js http-request chunked

我正在使用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();
});

0 个答案:

没有答案