最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to override express js default Keep Alive behavior - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

You have to set the header instead of remove Header

app.use(function(req, res, next) {
    res.setHeader('Connection', 'close');
    next();
});
发布评论

评论列表(0)

  1. 暂无评论