user2727195
user2727195

Reputation: 7340

node, is each request and response unique or cached irrespective of url

In an app that I was working, I encountered "headers sent already error" if I test using concurrency and parallel request methods.

ultimately I resolved the problem using !response.headersSent but my question is why am I forced to use it? is node caching similar requests and reuses them for the next repeated call.

    if(request.headers.accept == "application/json") {
        if(!response.headersSent) {response.writeHead(200, {'Content-Type': 'application/json'})}
        response.end(JSON.stringify({result:{authToken:data.authToken}}));
    }

Edit

var express = require('express');
var app = express();

var server = app.listen(process.env.PORT || 3000, function () {
    console.log('Example app listening at http://%s:%s', server.address().address, server.address().port);
});

Edit 2: Another problem is while testing using mocha, super agent and while the tests in progress if I just send another request through postman on the side, one of the tests in mocha end with a timeout error. These steps I'm taking to ensure the code is production ready for simultaneous, parallel requests? please advise on what measures I can take to ensure node/code works under stress.

Edit 3:

app.use(function(request, response, next){
    request.id = Math.random();
    next();
});

Upvotes: 0

Views: 931

Answers (1)

jfriend00
jfriend00

Reputation: 708036

OK, in an attempt to capture what solved this for you via all our conversation in comments, I will attempt to summarize here:

  1. The message "headers sent already error" is nearly always caused by improper async handling which causes the code to call methods on the response object in a wrong sequence. The most common case is non-async code that ends the request and then an async operation that ends some time later that then tries to use the request (but there are other ways to misuse it too).

  2. Each request and response object is uniquely created at the time each individual HTTP request arrives at the node/express server. They are not cached or reused.

  3. Because of asynchronous operations in the processing of a request, there may be more than one request/response object in use at any given time. Code that is processing these must not store these objects in any sort of single global variable because multiple ones can be in the state of processing at once. Because node is single threaded, code will only be running on any given request at any given moment, but as soon as that code hits an async operation (and thus has nothing to do until the async operation is done), another request could start running. So multiple requests can easily be "in flight" at the same time.

  4. If you have a system where you need to keep track of multiple requests at once, you can coin a request id and attach it to each new request. One way to do that is with a few lines of express middleware that is early in the middleware stack that just adds a unique id property to each new request.

  5. One simple way of coining a unique id is to just use a monotonically increasing counter.

Upvotes: 1

Related Questions