Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Express specific timeout value per route

I'm developing a Node v4.2.4 application using Express v4.13.4. Now I would like to increase the timeout time for a specific upload route.

From what I've read and experienced:

  • the default Node server timeout is 2 minutes
  • a Node socket has no timeout by default
  • there's middleware from Express to handle timeouts

However, I'm lost when trying to implement the connect-timeout middleware for the upload route.

Application setup

const app = express();
app.use(cors());
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
app.use(passport.initialize());

app.use('/uploads', uploadRoutes);
app.use(errorHandler);

function errorHandler(err, req, res, next) {
  if (err.code && err.code === 'ETIMEDOUT') {
    if (!res.headersSent) {
      res
        .status(408)
        .send({
          success: true,
          message: 'Timeout error'
        });
    }
  }

  next(err);
}

const server = app.listen(config.port);

Upload route definition

router.route('/:uploadId/upload-files')
  .post(timeout('3m'),
    require('./actions/upload-files').prepareHandler,
    require('./actions/upload-files').uploadHandler(),
    require('./actions/upload-files').responseHandler);

However, when uploading the files I do see the error from express-timeout after 3 minutes ONLY in the command line's console. The request is still in progress and no status code of 408 is returned.

After 4 minutes I finally see the 408 status and 'Timeout error' as part of the response object.

For requests to other routes I get the net::ERR_EMPTY_RESPONSE error after 4 minutes.

If I log the value for server.timeout, the value is 120000 (2 minutes).

My questions

  • where can the 4 minutes possible come from? Is it because there's a preceding OPTIONS request maybe?
  • what's the difference between server & socket timeouts and how to set them properly for a specific route?
like image 622
ndequeker Avatar asked Aug 10 '16 19:08

ndequeker


People also ask

How do I set timeout in Express?

const PORT = 3000; const server = app. listen(PORT); const timeout = 50*1000; // example timeout of 50 seconds server. setTimeout(timeout);

What is the default timeout in Express?

By default, normal HTTP requests to Node. js/Express/Sails. js apps time out after 2 minutes (120000 milliseconds) if a response is not sent.

What is request timeout in Nodejs?

In Node. js, default server timeout is 0 milliseconds for the latest version and 2min i.e. (120000 milliseconds) for the older version.

What is a route in Nodejs and Express?

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, which are executed when the route is matched.


1 Answers

Easy way to increase timeout on a route:

app.post('/some_route', (req, res) => {
   req.setTimeout(60 * 1000); //use req. this sets timeout to 60 seconds
});

OR

If you wanted to increase the timeout on the server for all routes, you can edit your app.js:

var serverApp = app.listen();
serverApp.setTimeout(60 * 1000); //use sets timeout to 60 seconds

Obviously, use the server setting sparingly. You don't want to tie up your server with long requests, so most of the time using the route specific timeouts is better architecture..

like image 90
GavinBelson Avatar answered Oct 12 '22 01:10

GavinBelson