微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Web服务 – 使用Node.js /强大的方式中止用户请求

我正在使用强大的接收文件上传与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(),但文件不断上传.

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

解决方法

问题是强大的不明白你想要停止.尝试这个:
req.connection.destroy();
req.connection.resume = function(){};

当然,这是一个很丑的解决方法,我是open an issue on github.

原文地址:https://www.jb51.cc/html/224592.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐