Express.js路由错误:发送后无法设置标题

我不确定为什么我得到这个错误。 这是一个简单的API构buildexpress.js能够添加和删除post。 当我触发删除路由器时发生错误。 我读过错误通常发生在有两个callback,但是,我似乎无法find任何双重callback。

_http_outgoing.js:344 throw new Error('Can\'t set headers after they are sent.'); Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11) at ServerResponse.header (/Users/bounty/Projects/_learning/react-express/node_modules/express/lib/response.js:718:10) at ServerResponse.send (/Users/bounty/Projects/_learning/react-express/node_modules/express/lib/response.js:163:12) at ServerResponse.json (/Users/bounty/Projects/_learning/react-express/node_modules/express/lib/response.js:249:15) at /Users/bounty/Projects/_learning/react-express/server/routes/posts.js:86:9 at nextTickCallbackWith0Args (node.js:452:9) at process._tickCallback (node.js:381:13) 

这是我的posts.js路由器:

 module.exports = function(router) { var Post = require('../models/post.js'); // middleware for the api requests router.use(function(req, res, next) { // do logging console.log('something is happening.'); next(); // make sure we go to our next route and don't stop here }); // test route to make sure everything is working (accessed at GET http://localhost:8080/api) router.get('/', function(req, res) { res.json({ message: 'hooray! welcome to our api!' }); }); // all routes here // routes that end in /posts router.route('/posts') // create a Post (accessed at POST http://localhost:7777/api/posts) .post(function(req, res) { var post = new Post(); post.postTitle = req.body.postTitle; // set the post name (comes from request) // save post and check for errors post.save(function(err) { if (err) res.send(); res.json({ message: 'post created!' }); }); }) // get all Posts (accessed at GET http://localhost:7777/api/posts) .get(function(req, res) { Post.find(function(err, posts) { if (err) res.send(); res.json(posts); }); }); // routes that end in /posts for specific id router.route('/posts/:post_id') // get the post with that id .get(function(req, res) { Post.findById(req.params.post_id, function(err, post) { if (err) res.send(err); res.json(post); }); }) // update the post with that id .put(function(req, res) { Post.findById(req.params.post_id, function(err, post) { if (err) res.send(err); post.postTitle = req.body.postTitle; // save the post post.save(function(err) { if (err) res.send(err); res.json({ message: 'post updated!' }); }); }); }) // deletes the post with that id .delete(function(req, res) { Post.remove({ _id: req.params.post_id }, function(err, post) { if (err) { res.send(err); } res.json({ message: 'post deleted!' }); }); }); } 

您需要添加“返回”,以便您不要回复两次。

 // save post and check for errors post.save(function(err) { if (err) { return res.send(); } res.json({ message: 'post created!' }); }); 

这个特定的错误信息几乎总是由于asynchronous响应的处理中的定时错误而导致的,这会导致您在响应已经发送之后尝试发送响应上的数据。

当人们将一个快速路由中的asynchronous响应当作同步响应,并且最终发送两次数据时,通常会发生这种情况。


我看到你的一个地方会得到这个是你的任何错误path:

当你这样做:

  // save post and check for errors post.save(function(err) { if (err) res.send(); res.json({ message: 'post created!' }); }); 

如果post.save()生成一个错误,你会做res.send() ,然后你会做res.json(...)之后。 你的代码需要有一个returnelse所以当出现错误时,你不要执行这两个代码path。

因此,当试图发送res.end两次res.sendres.json都发送的时候,这可能发生在Express中。 在你的if(err)块中,你会想return res.send()作为res.sendasynchronous运行, res.json也会被调用。 我想知道你的delete路线是否有错误? 希望这可以帮助。

最好!

您在同一请求中使用res.send()res.json()两次

这首先发送标题,然后是响应正文,然后再发送标题。 req.next通常不是函数, next是作为中间件的第三个parameter passing的。 如果你想下降到下一个中​​间件使用。 (假设你正在使用Express框架)

只是为了完整起见,我还会提到:有时问题可能在您可能正在使用的中间件中调用app.use

在检查以前的答案中提到的明显错误之后:

你应该删除所有的app.use语句,然后将它们逐一重新引入,以find有问题的模块。

  If you are using res.send() inside any loop, then you need to break it after the use of res.send(). So that it won't allow resetting of the res headers again and again. for eg : for(){ if(){ res.send(); break; } else(){ res.send(); break; } } In my case this is the problem and I solved it like this. Hope it may help someone in future. Thanks