我怎么知道请求是否有正文?

时间:2014-10-13 21:33:29

标签: node.js request

我有我为服务器编写的代码:

var http = require("http");
var url = require("url");

var routing = require("./RoutingPath");
var email = require('./NewServices.js');
var config = require ('./config.js');

function start() {  

    function onRequest(request, response) {

        var path = url.parse(request.url).pathname;
        var params = url.parse(request.url,true).query;

        if (request.url === '/favicon.ico')
        {
        }
        else
        {
            var data;
            request.on('data', function(chunk) {
              console.log("Received body data:");
              console.log(chunk.toString());
              data = chunk.toString();
            });

            routing.Route(path.toLowerCase(), params, data ,function(recordset){

                response.writeHead(200, {"Content-Type": "application/json"});

                if (recordset != null)
                {
                    if (recordset.respondMessage == "GeneralSqlError")
                    {
                        var msg = JSON.stringify(recordset, null, 4);
                        console.log(msg);
                        email.SendEmailErrorNotify(msg, function(err){
                            if(err)
                                console.log(err);
                        });
                    }
                    else
                    {
                        console.log(JSON.stringify(recordset, null, 4));
                    }

                    response.write(JSON.stringify(recordset, null, 4));
                }

                response.end();
                console.log();
            });

        }

  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started!");

}

有时帖子请求包含数据。当有一个身体我需要使用它并处理数据。

以下行中的变量“data”在调用时应该包含数据:

routing.Route(path.toLowerCase(), params, data ,function(recordset){
...

我怎么知道我是否有身体?

0 个答案:

没有答案