Node.js Using setKeepAlive() with express framework

Viewed 618

I want to use socket.setKeepAlive() in my application.

This is an example of using this function:

var net  = require('net');
var server = net.createServer(function(socket){
    socket.setKeepAlive(true,60000);

And another proper way to use this function:

var socket = net.connect(opts, function(){
    // 'connect' listener
    socket.setKeepAlive(true, 5000);
    socket.write("hello");
});

Since all this options use vanilla Node.js, how can i use this function with the express framework?

In express, i do not include the net module. Instead, i use this:

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

Based on this article:

https://github.com/expressjs/express/issues/3556

You could try using this express middleware:

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

app.use(function (req, res, next) {
  req.socket.setKeepAlive()
  next()
})
Related