使用 Node.js/formidable 中止用户请求

发布于 2024-11-08 19:21:04 字数 1178 浏览 0 评论 0原文

我正在使用 formidable 来接收使用 node.js 上传的文件。我在多部分请求中将一些字段与文件一起发送。

例如,一旦某些字段到达,我就能够验证请求的真实性,如果这不正确,我想中止整个请求,以避免浪费资源。

我还没有找到中止传入请求的正确方法。我尝试使用 req.connection.destroy(); 如下:

form
.on('field', function(field, value) {
    fields[field] = value;
    if (!fields['token'] || !fields['id'] || !fields['timestamp']) {
        return;
    }
    if (!validateToken(fields['token'], fields['id'], fields['timestamp'])) {
        res.writeHead(401, {'Content-Type' : 'text/plain' });
        res.end('Unauthorized');
        req.connection.destroy();
    }
})

但是,这会触发以下错误:

events.js:45
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Cannot resume() closed Socket.
    at Socket.resume (net.js:764:11)
    at IncomingMessage.resume (http.js:254:15)
    at IncomingForm.resume (node_modules/formidable/lib/incoming_form.js:52:11)
    at node_modules/formidable/lib/incoming_form.js:181:12
    at node_modules/formidable/lib/file.js:51:5
    at fs.js:1048:7
    at wrapper (fs.js:295:17)

我也尝试了 req.connection.end() 但文件持续上传。

有什么想法吗?提前致谢!

I'm using formidable to receive a file upload with node.js. I send some fields together with a file in a multipart request.

As soon as certain fields arrived, I'm able to validate the authenticity of the request for instance, and I would like to abort the whole request if this is not correct to avoid waisting resources.

I have not found a right way to abort the incoming request. I tried to use req.connection.destroy(); as follow:

form
.on('field', function(field, value) {
    fields[field] = value;
    if (!fields['token'] || !fields['id'] || !fields['timestamp']) {
        return;
    }
    if (!validateToken(fields['token'], fields['id'], fields['timestamp'])) {
        res.writeHead(401, {'Content-Type' : 'text/plain' });
        res.end('Unauthorized');
        req.connection.destroy();
    }
})

However, this triggers the following error:

events.js:45
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Cannot resume() closed Socket.
    at Socket.resume (net.js:764:11)
    at IncomingMessage.resume (http.js:254:15)
    at IncomingForm.resume (node_modules/formidable/lib/incoming_form.js:52:11)
    at node_modules/formidable/lib/incoming_form.js:181:12
    at node_modules/formidable/lib/file.js:51:5
    at fs.js:1048:7
    at wrapper (fs.js:295:17)

I also tried req.connection.end() but the file keeps uploading.

Any thoughts? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-11-15 19:21:04

问题是强大不明白你想让它停止。试试这个:

req.connection.destroy();
req.connection.resume = function(){};

当然,这是一个有点丑陋的解决方法,我在 github 上打开一个问题。

The problem is that formidable didn't understand that you want it to stop. Try this:

req.connection.destroy();
req.connection.resume = function(){};

Of course, this is a somewhat ugly workaround, I'd open an issue on github.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文