7

I'm trying to disable the keep-alive setting in a very simple express app. I tried the answer from someone doing the same in an older version:

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

app.get('/', function(req, res) {
    res.set('Connection', 'close'); // attempting to override default (keep-alive)
    res.set('Proof', 'close');      // just to show that this should modify the header value
    res.send('hello');
});

var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

However, Connection is still set to keep-alive:

$ curl -D - http://my-test-site.com
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Sun, 11 May 2014 08:07:51 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5
Connection: keep-alive
X-Powered-By: Express
Proof: close
ETag: "907060870"

hello%

How can I change keep-alive to close?

Versions

node 0.10.8
express 4.1.1

Community
  • 1
  • 1
Dean
  • 8,632
  • 6
  • 45
  • 61
  • Have you tried `res.set("KeepAlive", false)`? Here are the docs for Node http://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay ... I'm not very familiar with express. – Iansen May 11 '14 at 08:51
  • res.set('KeepAlive'...) is wrong answer. it set a new header filed which name is "KeepAlive" and the value is "false" – sarkiroka Oct 05 '16 at 09:17

1 Answers1

5

It works fine for me. I test it localhost the same version.

$ curl -D - 127.0.0.1:3000
HTTP/1.1 200 OK
X-Powered-By: Express
Connection: close
Proof: close
Content-Type: text/html; charset=utf-8
Content-Length: 5
ETag: "907060870"
Date: Sun, 11 May 2014 08:52:32 GMT

hello%

I see that you may use nginx to proxy. Like below:

client--nginx proxy--real server

I guess that you should disable keep-alive in nginx config file.

Tinple
  • 1,261
  • 1
  • 11
  • 11