【发布时间】:2014-08-28 06:54:09
【问题描述】:
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>');
现在它会抛出:
Error: Can't set headers after they are sent
我知道 nodejs 和 express 已经更新。为什么现在不能这样做?还有什么想法吗?
找到了解决方案,但 res.write 不在 api 参考中 http://expressjs.com/4x/api.html
【问题讨论】:
-
API参考页上有一行写着“res对象是Node自带的response对象的增强版,支持所有内置的字段和方法”,response.write是内置的方法。