Node.js 为什么可以';我们不能在Express.js中发送多个response.send吗?

Node.js 为什么可以';我们不能在Express.js中发送多个response.send吗?,node.js,express,Node.js,Express,3年前,我可以在express.js中发送多个res.send。 甚至写一个setTimeout来显示实时输出 response.send('<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>'); response.send('<html><body><input id="text_box" /><button>s

3年前,我可以在express.js中发送多个res.send。
甚至写一个setTimeout来显示实时输出

response.send('<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>');
response.send('<html><body><input id="text_box" /><button>submit</button></body></html>');
var initJs = function() {
  $('.button').click(function() {
    $.post('/input', { input: $('#text_box').val() }, function() { alert('has send');});
  });
}
response.send('<script>' + initJs + '</script>');
我知道nodejs和express已经更新了。为什么现在不能这样做?还有别的想法吗


找到解决方案,但“res.write”不在api引用中


:S

响应。send
向客户端发送整个HTTP响应,包括标题和内容,这就是您无法多次调用它的原因。事实上,它甚至会结束响应,因此在使用
response.send
时,无需显式调用
response.end

在我看来,您正试图像使用缓冲区一样使用
send
:向其写入,以便稍后刷新。然而,这不是该方法的工作原理;您需要在代码中建立响应,然后进行一次
send
呼叫


不幸的是,我无法解释为什么或何时进行此更改,但我知道至少从Express 3开始就一直如此。

也许您需要:
响应。编写

response.write("foo");
response.write("bar");
//...
response.end()

res.send
隐式调用
res.write
,后跟
res.end
。如果您多次调用
res.send
,它将在第一次工作。但是,由于第一个
res.send
调用结束了响应,因此您无法在响应中添加任何内容。

是的,听起来他可能就是这么想的。我完全忘记了Node的
响应;我的大脑在快速陆地上:/Cool,这是我想要的。但是,为什么它不在api参考中呢???也许是因为我们应该使用
res.render
res.send
,我还是新手的时候使用
res.write
@emj365为什么不试试
res.render
respone.write
response.writeHead
来自
http.ServerResponse
api在api参考页上有一行写着“res对象是节点自身响应对象的增强版本,支持所有内置字段和方法”,response.write是一个内置方法。
response.write("foo");
response.write("bar");
//...
response.end()