最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Drop request in node.js express - Stack Overflow

programmeradmin1浏览0评论

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, then return; or just an empty route handler should work fine. – Jonathan Lonowski Commented Nov 20, 2015 at 6:16
Add a ment  | 

3 Answers 3

Reset to default 3

To 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()

发布评论

评论列表(0)

  1. 暂无评论