admin管理员组文章数量:1425200
Is it possible using Node.js and express to drop a request for certain route? I.E. not return a http status or any headers? I'd like to just close the connection.
app.get('/drop', function(req, res) {
//how to drop the request here
});
Is it possible using Node.js and express to drop a request for certain route? I.E. not return a http status or any headers? I'd like to just close the connection.
app.get('/drop', function(req, res) {
//how to drop the request here
});
Share
Improve this question
asked Nov 20, 2015 at 5:41
JustinJustin
45.5k83 gold badges213 silver badges312 bronze badges
3
-
2
I am not sure, just wondering. Doesn't
return;
produce the desired result? It sounds logical - simply do nothing with response. – Yeldar Kurmangaliyev Commented Nov 20, 2015 at 5:48 - I don't think return will drop the client request, still pends. – Justin Commented Nov 20, 2015 at 5:49
-
5
To drop the connection, you should be able to
.destroy()
the.socket
used by the request –req.socket.destroy()
. This will emulate a "disconnect" rather than a "timeout," as you mentioned in other ments. If you want to prolong the request on the server indefinitely, letting the client eventually choose to timeout, thenreturn;
or just an empty route handler should work fine. – Jonathan Lonowski Commented Nov 20, 2015 at 6:16
3 Answers
Reset to default 3To close a connection without returning anything, you can either end()
or destroy()
the underlying socket.
app.get('/drop', function(req, res) {
req.socket.end();
});
I don't think there's any way to drop the connection at your end but keep the client waiting until it times out (i.e. without sending a FIN
). You'd perhaps have to interact with your firewall in some way.
Yes you can. All you need to do is call the res.end method optionally passing in the status code.
Use one of the following methods:
res.end();
res.status(404).end();
If you wanted to also set the headers, then you'd use the res.set method. See below
res.set('Content-Type', 'text/plain');
res.set({
'Content-Type': 'text/plain',
'Content-Length': '123',
'ETag': '12345'
})
For details have a look here http://expressjs./api.html
You could do this wherever you want to close the connection:
res.end()
本文标签: javascriptDrop request in nodejs expressStack Overflow
版权声明:本文标题:javascript - Drop request in node.js express - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745404212a2657176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论