使用multer上传文件时重置Node.js连接 [英] Node.js connection reset on file upload with multer

查看:78
本文介绍了使用multer上传文件时重置Node.js连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点/快速应用程序,可以通过multer处理文件上传.在我的本地计算机上一切正常,但是在服务器上,如果上载的文件超过几个Mb,浏览器将因连接重置"错误而停止.

I have a node/express app that handles file uploads with multer. Everything works well on my local machine, but on the server, if the uploaded file exceeds a couple of Mbs, the browser stops with a "connection reset" error.

这是上传脚本的一个简单测试版本:

Here is a simple test version of the upload script:

var express = require('express');
var multer = require('multer');

// Create server
var app = express();

// Start server
function startServer() {
    var port = 8888;
    server = app.listen(port, function () {
        console.log('Node version:' + process.versions.node);
        console.log('Express server listening on port %d in %s mode', port, app.settings.env);
    });
}

var upload = multer({dest: './tmp/'});

var app = express()

app.post('/', upload.single('data'), function (req, res, next) {
    console.log(req.file);
});

startServer();

这是测试上传的html页面:

And here's the html page to test the upload:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Test Upload</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

    </head>
    <body>
        <p>Hello world! This is a test upload.</p>

        <form method="post" action="http://192.168.1.234:8888" enctype="multipart/form-data">
            <label>file</label><br>
            <input type="file" name="data"><br>

            <input type="submit" name="submit" value="Upload">
        </form>


    </body>
</html>

我在两台不同的服务器(VPS和裸机)上进行了测试,结果在两台服务器上都出现了相同的错误.上传开始,我可以在 ./tmp 目录中看到该文件的块,但是它永远不会完成,也不会在节点或系统日志中引发任何错误.

I tested on two different servers – a VPS and a bare-metal box – I ended up with the same error on both servers. The upload starts and I can see a chunck of the file in my ./tmp directory, but it never finishes and doesn't throw any error, neither in node nor in the syslog.

推荐答案

@NadavL是正确的,尽管事实上我没有前端服务器,但节点本身正在超时.

@NadavL was right, despite the fact that I had no front server, node was timing out itself.

它是用文档编写的,默认情况下,节点中没有超时.Express可能会覆盖该问题,但我找不到此事的任何信息.

It's written in the docs that there's no timeout in node by default. Express might override that but I couldn't find any information on the matter.

要全局定义特定的超时,您可以通过在服务器连接时更改套接字超时来继续

To define a specific timeout globally, you can proceed by changing the socket timeout when the server connects

[…]

// Start server
function startServer() {
    var port = 8888;
    server = app.listen(port, function () {
        console.log('Node version:' + process.versions.node);
        console.log('Express server listening on port %d in %s mode', port, app.settings.env);
    });

    server.on('connection', function(socket) {
        // 10 minutes timeout
        socket.setTimeout(10 * 60 * 1000);
    });
}

但是,随着超时时间的延长,您的服务器可能会受到 HTTP慢速攻击,您可能希望仅针对特定路由更改默认超时,在我的情况下,仅针对上载路由.在这种情况下,您要做的就是更改路由处理程序中的超时,如下所示:

But as a high timeout rises your server's exposure to Slow HTTP Attacks, you might want to change the default timeout just for a specific route – in my case, just for the upload route. In this particular case, all you have to do is change the timeout in your route's handler like so:

app.post('/myroute', function (req, res) {
    // 10 minutes timeout just for POST to myroute
    req.socket.setTimeout(10 * 60 * 1000);
    upload.single('data');
    console.log(req.file);
});

还有一个专用的中间件来处理超时,它被称为 connect-time ,并且可以使用它也可以为不同的路由配置特定的超时(请参见这样的帖子).

There's also a dedicated middleware to handle timeouts, it's called connect-timeout and it can be used to configure specific timeouts for different routes too (see this post on SO).

这篇关于使用multer上传文件时重置Node.js连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆