Express.js接近响应 [英] Express.js close response

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

问题描述

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



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

  String line; 
while((line = in.readLine())!= null){
System.out.println(RES:+ line);
}

但这只是保持挂起..没有结束连接,仍然等待输入



节点:

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

然而 res.end()不关闭连接..如前所述,java一直认为下一个会有一些东西,所以它被卡在while循环中。



谢谢


解决方案

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

  res.set(连接,关闭); 


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

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.

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.

Thank you

解决方案

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天全站免登陆