title: Connection:keep-alive is not keeping the socket connection for HTTP request in NodeJS
body: <p>I have heard that <code>Connection:Keep-Alive</code> header will tell the server to keep the connection between client and server for a while to prevent the effort for each time client establish a request to server. I tried to add that to the request's header but it didn't work out as expected. The socket connection still close on each request.</p>
  
  <p>Could you please help to explain why that happened? Did I missing something about <code>Connection:Keep-Alive</code> or did I implement it the wrong way?</p>
  
  <p>Client:</p>
  
  <pre><code>const http = require("http");
  
  const options = {
    port: 4000,
    headers: {
      connection: "keep-alive",
    },
  };
  
  function request() {
    console.log(`making a request`);
  
    const req = http.request(options, (res) =&gt; {
      console.log(`STATUS: ${res.statusCode}`);
      console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  
      res.setEncoding("utf8");
      res.on("data", (chunk) =&gt; {
        console.log(`BODY: ${chunk}`);
      });
      res.on("end", () =&gt; {
        console.log("No more data in response.");
      });
    });
  
    req.on("error", (e) =&gt; {
      console.error(`problem with request: ${e.message}`);
    });
  
    req.end();
  }
  
  setInterval(() =&gt; {
    request();
  }, 3000);
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>const http = require("http");
  
  const server = http.createServer((req, res) =&gt; {
    setTimeout(() =&gt; {
      res.end();
    }, 500);
  });
  
  server.on("connection", function (socket) {
    socket.id = Date.now();
    console.log(
      "A new connection was made by a client." + ` SOCKET ${socket.id}`
    );
    socket.on("end", function () {
      console.log(
        `SOCKET ${socket.id} END: other end of the socket sends a FIN packet`
      );
    });
  
    socket.on("timeout", function () {
      console.log(`SOCKET ${socket.id} TIMEOUT`);
    });
  
    socket.on("error", function (error) {
      console.log(`SOCKET ${socket.id} ERROR: ` + JSON.stringify(error));
    });
  
    socket.on("close", function (had_error) {
      console.log(`SOCKET ${socket.id} CLOSED. IT WAS ERROR: ` + had_error);
    });
  });
  server.on("clientError", (err, socket) =&gt; {
    socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
  });
  
  server.listen(4000);
  </code></pre>
  
  <p>And this is what I've got:</p>
  
  <p>Client:</p>
  
  <pre><code>making a request
  STATUS: 200
  HEADERS: {"date":"Thu, 09 Apr 2020 13:05:44 GMT","connection":"keep-alive","content-length":"81"}
  No more data in response.
  
  making a request
  STATUS: 200
  HEADERS: {"date":"Thu, 09 Apr 2020 13:05:47 GMT","connection":"keep-alive","content-length":"81"}
  No more data in response.
  
  making a request
  STATUS: 200
  HEADERS: {"date":"Thu, 09 Apr 2020 13:05:50 GMT","connection":"keep-alive","content-length":"81"}
  No more data in response.
  </code></pre>
  
  <p>and Server:</p>
  
  <pre><code>A new connection was made by a client. SOCKET 1586437543111
  { connection: 'keep-alive', host: 'localhost:1234' }
  SOCKET 1586437543111 END: other end of the socket sends a FIN packet
  SOCKET 1586437543111 CLOSED. IT WAS ERROR: false
  
  A new connection was made by a client. SOCKET 1586437546091
  { connection: 'keep-alive', host: 'localhost:1234' }
  SOCKET 1586437546091 END: other end of the socket sends a FIN packet
  SOCKET 1586437546091 CLOSED. IT WAS ERROR: false
  
  A new connection was made by a client. SOCKET 1586437549095
  { connection: 'keep-alive', host: 'localhost:1234' }
  SOCKET 1586437549095 END: other end of the socket sends a FIN packet
  SOCKET 1586437549095 CLOSED. IT WAS ERROR: false
  </code></pre>
  
  <p>NodeJS version: 10.15.0</p>
  
  <hr>
  
  <p>There is one more thing makes me even more confuse is that when I use <code>telnet localhost 1234</code> to make a request:</p>
  
  <pre><code>GET / HTTP/1.1
  Connection: Keep-Alive
  Host: localhost
  </code></pre>
  
  <p>Then the connection is not close, and no new connection being created as expected. Is that because telnet receive <code>Connection: Keep-Alive</code> and keep the connection by itself?</p>