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.
是否可以使用Node.js和express来删除某个路由的请求? I.E.不返回http状态或任何标题?我想关闭连接。
app.get('/drop', function(req, res) {
//how to drop the request here
});
3 个解决方案
#1
0
To close a connection without returning anything, you can either end()
or destroy()
the underlying socket.
要在不返回任何内容的情况下关闭连接,可以使用end()或destroy()底层套接字。
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.
我认为没有办法在你的终端断开连接,但让客户端等到它超时(即没有发送FIN)。您可能需要以某种方式与防火墙进行交互。
#2
-1
Yes you can. All you need to do is call the res.end method optionally passing in the status code.
是的你可以。您需要做的就是调用res.end方法,可选择传入状态代码。
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方法。见下文
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
详情请看http://expressjs.com/api.html
#3
-1
You could do this wherever you want to close the connection: res.end()
您可以在任何想要关闭连接的地方执行此操作:res.end()
#1
0
To close a connection without returning anything, you can either end()
or destroy()
the underlying socket.
要在不返回任何内容的情况下关闭连接,可以使用end()或destroy()底层套接字。
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.
我认为没有办法在你的终端断开连接,但让客户端等到它超时(即没有发送FIN)。您可能需要以某种方式与防火墙进行交互。
#2
-1
Yes you can. All you need to do is call the res.end method optionally passing in the status code.
是的你可以。您需要做的就是调用res.end方法,可选择传入状态代码。
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方法。见下文
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
详情请看http://expressjs.com/api.html
#3
-1
You could do this wherever you want to close the connection: res.end()
您可以在任何想要关闭连接的地方执行此操作:res.end()