Skip to content Skip to sidebar Skip to footer

Drop Request In Node.js Express

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',

Solution 1:

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.

Solution 2:

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.com/api.html

Solution 3:

You could do this wherever you want to close the connection: res.end()

Post a Comment for "Drop Request In Node.js Express"