【发布时间】:2016-01-16 10:23:00
【问题描述】:
我正在玩弄这个 node.js 服务器应用程序,它具有用于自制 REST API 和服务静态页面的路由。当它为静态页面提供服务时,我没有收到任何错误,但是当它从我的 API 提供内容时,我在控制台中得到以下信息:
Error: Can't set headers after they are sent.
at SendStream.headersAlreadySent (C:\path_to_node_folder\node_modules\send\lib\send.js:302:13)
at SendStream.send (C:\path_to_node_folder\node_modules\express\node_modules\send\lib\send.js:490:17)
at C:\path_to_node_folder\node_modules\express\node_modules\send\lib\send.js:467:10
at FSReqWrap.oncomplete (fs.js:82:15)
我的路线:
module.exports = function(app) {
"use strict";
app.get('/api', function(req, res, next) {
res.json({
message : 'Welcome to the Angular Blog RESTful API'
});
next();
});
app.get('/api/article/:permalink', function(req, res, next) {
res.json({
message : "Article retrieved",
permalink : req.params.permalink
});
next();
});
app.post('/api/comment', function(req, res, next) {
res.json({
message : "save comment"
});
next();
});
app.use(function(req, res) {
// use res.sendfile, as it streams instead of reading the file into memory.
res.sendfile('./public_html/index.html');
});
};
现在,请不要讨厌返回的 JSON 数据,我现在只是在搞乱它:)
问题:如何摆脱上述控制台错误?
【问题讨论】:
标签: javascript json node.js routes