Express.js 关闭响应 [英] Express.js close response

查看:43
本文介绍了Express.js 关闭响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法关闭回复?我可以使用 res.end() 但它实际上并没有关闭套接字.

Is there a way to close a response? I can use res.end() but it doesn't actually close the socket.

我想要实现的目标:我正在编写一个与网络接口的 Java 程序,我正在为此编写一个 node.js 服务器.Java代码:

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:

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.

节点:

exports.getAll = function (req, res) {
    res.set("Content-Type", "text/plain");
    res.set(200);
    res.send(..data..);
    res.end();
}

然而 res.end() 并没有关闭连接.. 如前所述,java 一直认为接下来会发生一些事情,所以它被卡在了 while 循环中.

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.

谢谢

推荐答案

通过设置 HTTP 头来关闭连接而不是默认的 keep-alive 策略解决.

Solved by setting a HTTP header to close the connection instead of default keep-alive strategy.

res.set("Connection", "close");

这篇关于Express.js 关闭响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆