【问题标题】:Abort user request with Node.js/formidable使用 Node.js/formidable 中止用户请求
【发布时间】:2011-05-21 05:00:30
【问题描述】:

我正在使用强大的 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(),但文件一直在上传。

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

【问题讨论】:

  • 我也对此感兴趣。你找到解决办法了吗?
  • 我几乎准备好为这个问题写一个重复的问题?到目前为止有什么想法吗?

标签: web-services http node.js httprequest


【解决方案1】:

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

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

当然,这是一个有点丑陋的解决方法,我会open an issue on github

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 2018-03-11
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2014-10-11
    相关资源
    最近更新 更多