简体   繁体   中英

node.js, http and making multiple calls to the same function from multiple request. Seems to be corrupting data

I may not quite understand why this is happening, but I'd like some assistance with this issue to help me better understand the processing of node.js requests using http and calling the same function.

This is simulating a node function I want to use when incoming web requests are made, but I want it to use separate resources for each call (if that's the right term). So for each request, each call should only touch it's own data.. while it seems that isn't happening and each call is stepping on each other's other toes.

The setTimeout is used to simulate a process that may take a while.

server code:

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

http.createServer(function(req, res){
  pathName= url.parse(req.url).pathname;
  var rc = "";

  ts.process(function(rc) {
    console.log(rc + " all done.");
  });

  res.writeHead(200, {"Content-type":"text/plan"});
  res.end("thanks for the data!");

}).listen(5250, "192.168.201.40");

console.log("Listening on 192.168.201.40" + ":" + "5250");

"test" code from require:

var timeStamp;

function process(callback) {
  timeStamp = Date.now();
  console.log("test-in: " + timeStamp);
  setTimeout(function() {
    console.log("test-out: " + timeStamp);
    callback(timeStamp);
  }, 5000);
}

module.exports = {
  process: process
}

Client request code:

var http = require('http');
var postData = "postdata";

var options = {
  host: "192.168.201.40",
  port: 5250,
  path: '/',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
  }
};

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  res.on('data', function(chunk) {
    console.log('Response: ' + chunk);
  });
});

req.write(postData);
req.end();

When I call the test sample code once to make the request, I get what I would expect:

test-in: 1522107154345
test-out: 1522107154345
1522107154345 all done.

But, if I call the test function 3 times before the other function calls are done, I get this:

test-in: 1522107251094
test-in: 1522107251969
test-in: 1522107252939
test-out: 1522107252939
1522107252939 all done.
test-out: 1522107252939
1522107252939 all done.
test-out: 1522107252939
1522107252939 all done.

When I expect to get:

test-in: 1522107251094
test-in: 1522107251969
test-in: 1522107252939
test-out: 1522107251094
1522107251094 all done.
test-out: 1522107251969
1522107251969 all done.
test-out: 1522107252939
1522107252939 all done.

I'm sure it's something I just simply don't understand yet with javascript asyc programming.

Your issue comes from the fact that the variable timestamp is defined outside of the test closure. Having it there you are modifying the global state, or state shared between method calls, in which case the result is unpredictable. Define it within the function process so that its value is included in the closure accessible by the callback and you will fix the behaviour:

fun

ction process(callback) {
  var timeStamp = Date.now();
  console.log("test-in: " + timeStamp);
  setTimeout(function() {
    console.log("test-out: " + timeStamp);
    callback(timeStamp);
  }, 5000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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