【问题标题】:How to extend the response timeout default time?如何延长响应超时默认时间?
【发布时间】:2017-10-30 19:53:20
【问题描述】:

我在使用线程阻塞算法时遇到了一点问题。

我有一条路线可以生成包含大文件的 zip。

流程如下: GetUrls > 获取HugeSizeBuffer > GenerateZIP > UploadZIPToCloud

我无法使用 express-timeout 模块修改 2 分钟的超时响应默认时间。我也一直试图打破.nextTick() 函数中的漏洞。

我什至尝试过检查排队,但我认为这不适用于这种情况。

你们知道如何延长响应时间吗? - 我只需要一条路线。

【问题讨论】:

    标签: javascript node.js multithreading express blocking


    【解决方案1】:
    // start the server
    const server = app.listen(8080);
    
    // increase the timeout to 4 minutes
    server.timeout = 240000;
    

    这是延长服务器超时的最简单方法,但它会影响一切,而不仅仅是一种方法。

    在你的情况下(你不希望它只用于特定路线):

    '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;
    

    不是我的代码,在这个主题中找到它:Node Express specific timeout value per route

    你需要更好地谷歌:)

    【讨论】:

    • 我只需要一条路线。
    • 已编辑答案,所以这应该对您有所帮助
    • 好的,谢谢,我试试。购买为什么 timeout-express 在这种情况下不起作用,这很奇怪......
    • 我用过这个解决方案LINK
    • 你的回答很有魅力。非常感谢!看来我真的需要进一步提高我的搜索技能,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多