Node.js 设置HTTPS超时的正确方法

Node.js 设置HTTPS超时的正确方法,node.js,express,ssl,https,timeout,Node.js,Express,Ssl,Https,Timeout,我有一个工作正常的上传系统,但由于我在我的express服务器上实现了SSL,任何需要超过2分钟的上传都会失败,我得到了net::ERR_FAILED,就像以前一样,任何大小的文件都可能需要花费它想要的时间,最终到达那里,但现在正好需要2分钟,它就停止了 我查看了一堆关于节点中HTTPS超时的帖子 我尝试了很多不同的超时设置,但似乎都不起作用 我尝试过的事情的例子: app.post( '/uploadResources/', extendTimeout, uploadResources.a

我有一个工作正常的上传系统,但由于我在我的express服务器上实现了SSL,任何需要超过2分钟的上传都会失败,我得到了
net::ERR_FAILED
,就像以前一样,任何大小的文件都可能需要花费它想要的时间,最终到达那里,但现在正好需要2分钟,它就停止了

我查看了一堆关于节点中HTTPS超时的帖子

我尝试了很多不同的超时设置,但似乎都不起作用

我尝试过的事情的例子:

app.post(
  '/uploadResources/', extendTimeout, uploadResources.array('file', 1), (req, res, next) => {
    
    console.log("File must be done if you can read this...")
    req.socket.setTimeout(500000);
    res.json({
      file: req.file,
    });
    req.end();
});

function extendTimeout (req, res, next) {
  console.log("Started!");
  // adjust the value for the timeout, here it's set to 3 minutes
  res.setTimeout(500000, () => { console.log("timed out") })
  next();
}
在我的帖子路线中:

app.post(
  '/uploadResources/', extendTimeout, uploadResources.array('file', 1), (req, res, next) => {
    
    console.log("File must be done if you can read this...")
    req.socket.setTimeout(500000);
    res.json({
      file: req.file,
    });
    req.end();
});

function extendTimeout (req, res, next) {
  console.log("Started!");
  // adjust the value for the timeout, here it's set to 3 minutes
  res.setTimeout(500000, () => { console.log("timed out") })
  next();
}
使用这些变体:

app.post(
  '/uploadResources/', extendTimeout, uploadResources.array('file', 1), (req, res, next) => {
    
    console.log("File must be done if you can read this...")
    req.socket.setTimeout(500000);
    res.json({
      file: req.file,
    });
    req.end();
});

function extendTimeout (req, res, next) {
  console.log("Started!");
  // adjust the value for the timeout, here it's set to 3 minutes
  res.setTimeout(500000, () => { console.log("timed out") })
  next();
}
res.connection.setTimeout(0)//无限

res.connection.setTimeout(500000)

在我定义服务器的地方,我尝试了所有这些:

app.post(
  '/uploadResources/', extendTimeout, uploadResources.array('file', 1), (req, res, next) => {
    
    console.log("File must be done if you can read this...")
    req.socket.setTimeout(500000);
    res.json({
      file: req.file,
    });
    req.end();
});

function extendTimeout (req, res, next) {
  console.log("Started!");
  // adjust the value for the timeout, here it's set to 3 minutes
  res.setTimeout(500000, () => { console.log("timed out") })
  next();
}
server.setTimeout(500000)

server.timeout=500000

server.on('connection', function(socket) {
        socket.setTimeout(500000);
        socket.setKeepAlive(true);
    });
所有这些都没有改变!无论如何,在添加SSL证书并使用HTTP之前,我对服务器的定义如下:

var app = express();

// Listen on port 3000 for any calls
var server = app.listen(3000, function () {
  console.log('VCS-API REST server started - yay.');
 });
 server.on('connection', function(socket) {
    
   // 10 minutes timeout
         socket.setTimeout(500000);
         socket.setKeepAlive(true);
     });
这很有效。。。但是,我添加了我的SSL证书,现在我这样定义了服务器(对于在不到2分钟内完成的较小文件,这很好):


因此,我的确切问题是:在HTTPS中设置超时以实现更长的文件上载的正确方法是什么?

因此,我通过op对此进行了一些有趣的调试,结果发现使用包“”进行了第二次连接

这是出现问题的连接,而不是从browser->node上载文件

要公开问题,添加了的事件侦听器:

server.on('clientError', (err, socket) => {
  console.log(err)
});
这显示了超时错误

ERR_HTTP_REQUEST_TIMEOUT
未处理的错误导致整个连接关闭。简单地处理事件中的错误就足以让请求按预期完成


如果不深入研究multer-s3打包,那么当底层服务器是https时,似乎会出现某种超时问题。对此的修复方法是捕获错误,记录错误并允许请求继续。

您使用的是哪个节点版本?@eol-Im使用v14.11.0w
server.setTimeout()
对我来说效果很好-客户端和你的节点应用程序之间是否有负载平衡器或类似的东西?它在AWS上,前端在cloudfront上,但API在ec2服务器上,我的前端应用程序通过专用子域(不受cloudfront控制)调用该服务器,但为了回答你的问题,不,我没有任何负载平衡设置