I'm been doing some stress testing on nodejs express server and noticed that it includes a "Connection: Keep-Alive" header by default. My application will be a web api exposed to the client, so I don't need the connection to remain open after the client has posted it's data and received it's response.
Unfortunately due to client limitations this needs to be a synchronous operation. We already have a C# Web API that we intend to replace by a nodejs server and this API don't a append a Keep Alive header on the HTTP response. I can't find a way to override this behavior. I've tried using app.disable and also removing the header on the middleware, but it doesn't work (it works for other HTTP headers though).
var express = require('express'),
app = express(),
port = 58458;
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});
app.use(function (req, res, next) {
res.removeHeader('Connection','Close');
next();
});
app.disable('Connection');
var counter = 0;
app.listen(port, function() { console.log('escutando') } );
app.post("/api/order", function(req, res) {
counter = counter + 1;
var r = req.body;
if (counter%100 === 0)
console.log(counter);
res.json(1);
res.end();
});
I'm been doing some stress testing on nodejs express server and noticed that it includes a "Connection: Keep-Alive" header by default. My application will be a web api exposed to the client, so I don't need the connection to remain open after the client has posted it's data and received it's response.
Unfortunately due to client limitations this needs to be a synchronous operation. We already have a C# Web API that we intend to replace by a nodejs server and this API don't a append a Keep Alive header on the HTTP response. I can't find a way to override this behavior. I've tried using app.disable and also removing the header on the middleware, but it doesn't work (it works for other HTTP headers though).
var express = require('express'),
app = express(),
port = 58458;
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});
app.use(function (req, res, next) {
res.removeHeader('Connection','Close');
next();
});
app.disable('Connection');
var counter = 0;
app.listen(port, function() { console.log('escutando') } );
app.post("/api/order", function(req, res) {
counter = counter + 1;
var r = req.body;
if (counter%100 === 0)
console.log(counter);
res.json(1);
res.end();
});
Share
Improve this question
asked Feb 27, 2014 at 12:00
Rodrigo ValladaresRodrigo Valladares
211 silver badge3 bronze badges
1 Answer
Reset to default 9You have to set the header instead of remove Header
app.use(function(req, res, next) {
res.setHeader('Connection', 'close');
next();
});