Javascript Express.js 关闭响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13554319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express.js close response
提问by Matej
Is there a way to close a response? I can use res.end()but it doesn't actually close the socket.
有没有办法关闭响应?我可以使用,res.end()但它实际上并没有关闭套接字。
What I want to achieve: I am writing a Java program which interfaces with the network, and I am writing a node.js server for this. Java code:
我想要实现的目标:我正在编写一个与网络接口的 Java 程序,我正在为此编写一个 node.js 服务器。爪哇代码:
String line;
while((line = in.readLine()) != null) {
System.out.println("RES: "+line);
}
But this just keeps hanging.. No end connection, still waiting for input from the socket.
但这只是一直挂着..没有结束连接,仍在等待来自套接字的输入。
Node:
节点:
exports.getAll = function (req, res) {
res.set("Content-Type", "text/plain");
res.set(200);
res.send(..data..);
res.end();
}
however res.end()does not close the connection.. As said before, java keeps thinking there will be something next so it is stuck in the while loop.
但是res.end()并没有关闭连接.. 如前所述,java 一直认为接下来会有一些事情,所以它被困在 while 循环中。
Thank you
谢谢
回答by Matej
Solved by setting a HTTP header to close the connection instead of default keep-alive strategy.
通过设置 HTTP 标头来关闭连接而不是默认的保持活动策略来解决。
res.set("Connection", "close");
回答by Spect
In case someone who really wants just to close the connection finds this, you can use res.socketor res.connectionto get the net.Socketobject which comes with a destroymethod:
如果真的只想关闭连接的人发现了这一点,您可以使用res.socket或res.connection来获取带有destroy方法的net.Socket对象:
res.connection.destroy();

