如何在node.js/express中用json生成多个响应.write:s
How to generate multiple response.write:s with json in node.js/express?
在生成多个响应时遇到一些问题。请从node.js中的api调用中写入:s。以下是代码。
// get the articles
app.get('/api/articles', function(req, res) {
res.writeHead(200, {
"Content-Type": "application/json"
});
// use mongoose to get all feeds in the database
Feed.find(function(err, feeds) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err);
feeds.forEach(function(feedModel) {
//for each feed in db get articles via feed-read module
feed(feedModel.url, function(err, articles) {
articles.forEach(function(articleModel) {
console.log(JSON.stringify(articleModel));//works!!
res.write(JSON.stringify(articleModel));//doesnt produce output.
});
});
});
}); //end find function
res.end();
}); //end api call
您需要在末尾的回调no中end()
。
res.end(JSON.stringify(articleModel));
经过一些修补,我删除了res.writeHead和res.end行,使其正常工作。显然,我将用前面的代码触发两个writeHead,通过删除行节点或express将"自动"修复头。/Stefan
相关文章:
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- 如何将getJson的响应保存在全局变量中
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 使用angularjs向浏览器发送servlet响应(下载功能)
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 不再使用innerHTML进行AJAX响应.(代码未得到响应)
- 为什么可以't我使用了AJAX响应的一部分
- jQuery AJAX write to XML
- 响应动画手风琴不工作
- 如何从SeleniumWebdriver获取异步Javascript响应
- Ajax调用在Firefox中不会自动响应
- 可以在响应时隐藏iphone上的“播放”按钮以进行视频播放
- 具有javascript document.write的响应图像
- 响应.write与文档.write
- ajax成功中的广告调用响应包含document.write
- 我怎样才能使表单控件在“write back”中具有响应性?的方式
- res.write在nodejs中阻止服务器响应其他请求,直到你调用res.end
- 如何在node.js/express中用json生成多个响应.write:s
- 为什么无法使用response.writeHead和response.write编写响应