Node Express specific timeout value per route

前端 未结 1 1456
一个人的身影
一个人的身影 2020-12-10 14:44

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

相关标签:
1条回答
  • 2020-12-10 15:01

    I've solved it using the following route configuration:

    'use strict';
    
    const ms = require('ms');
    const express = require('express');
    const router = express.Router();
    
    router.route('/upload-files')
      .post(
        setConnectionTimeout('12h'),
        require('./actions/upload-files').responseHandler
      );
    
    function setConnectionTimeout(time) {
      var delay = typeof time === 'string'
        ? ms(time)
        : Number(time || 5000);
    
      return function (req, res, next) {
        res.connection.setTimeout(delay);
        next();
      }
    }
    
    exports.router = router;
    

    Where the key logic is the following middleware:

      function (req, res, next) {
        res.connection.setTimeout(delay);
        next();
      }
    

    I hope this reference will come in handy for others.

    0 讨论(0)
提交回复
热议问题