Node JS HTTP Server request.on('data') 事件失败

Node JS HTTP Server request.on('data') event failure

本文关键字:data 失败 事件 HTTP JS Server request on Node      更新时间:2023-09-26

我正在开发一个NodeACS应用程序,其中我必须从Java HTTP客户端发送xml作为请求,并在经过一些操作后接收响应。到目前为止,Java HTTP客户端工作正常,但问题出在Node JS文件上。在我的情况下,req.on('data'...)事件没有触发。以下是我的JS文件的代码:

function index(req, res) {
    console.log(req.headers); //Its getting printed on console
    req.on('data', function (data) {
        console.log("Inside 1");     //Not printed on console
        ....REST OF THE CODE....
    });
    req.on('end', function () {
        res.writeHead(200);
        res.end();
    });
}

在上面的代码中,在获取请求后,索引函数被调用并打印控制台.log(req.headers)的输出;但如上所述,脚本在此之后不再运行。请帮助我解决我在这里缺少什么。

为了进行测试,我已经在我的系统上安装了 cURL,并使用以下命令使用 cURL 请求向服务器发送 POST 请求:

curl -X POST -d @output.xml http://localhost:7788/

你能确认你在此之前没有消耗身体吗?如果主体已经被另一个中间件(如主体解析器)使用,则需要通过连接-重流器之类的东西重新流式传输主体。如果它被使用,则不会发出数据事件。

如果签出,请检查是否发出了"结束"事件。可能是内容长度标头设置为错误值的标志。