简体   繁体   English

我如何知道哪个请求属于node.js中的部分数据

[英]How do I know which request belongs to what partial data in node.js

I have the node.js server below. 我在下面有node.js服务器。 Suppose that multiple clients make a POST request. 假设有多个客户端发出POST请求。 But these will share the same variable body and the final string will become meaningless. 但是这些将共享相同的变量主体,并且最后的字符串将变得毫无意义。 Is is possible to identify a request by id or something? 是否可以通过ID或其他方式识别请求? All I have is request.headers, but that does not contain any unique information. 我所拥有的只是request.headers,但是其中不包含任何唯一信息。

var http = require("http");
var server = http.createServer();

server.on("request", onRequest);
server.listen(8001, "127.0.0.1");

function onRequest(request, response) // assuming "POST"
{
    var body = "";

    request.on("data", function (data) { body = body + data; });
    request.on("end", function () { console.log(body); });
}

通常,函数会继承作用域(从其父级继承),因此将为每个请求正确地串联每个body变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM