res.redirect()在node.js中不起作用

我正在尝试发送/草稿请求,并在我的数据库中创build一个新的“草稿”/更新现有的草稿。 之后,我想立即redirect到/ draft?id = RELEVANT_ID_HERE页面。

这是我目前的POST请求function:

app.post('/draft', function(req,res){ var id = req.query.id; var postTitle = req.body.head; var article = req.body.article; if(id){ db.postCatalog.findOneAndUpdate({_id: id}, {title:postTitle, inShort:article.substring(0,100), content:article}, function(err, data){ if (err) { return handleError(err); } else { console.log(data); res.status(200).redirect('/draft?id='+id); } }); } else{ id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); new db.postCatalog({title:postTitle, _id:id, author:'temp', AuthorID:2, date:'2/3/12', inShort:article.substring(0,100), content:article , published:false }).save(function (err, data) { if (err) { return handleError(err); } else { console.log(data); res.status(200).redirect('/draft?id='+id); } }); } }); 

所以,除了redirect以外,所有的东西都可以工 我在节点控制台中获得正确的GET请求,但浏览器中没有任何反应。 节点控制台日志

这是GET请求的代码:

 app.get('/draft', function(req,res){ var id = req.query.id; if(id){ db.postCatalog.findOne({_id: id}, function(err, post){ if(err) { return handleError(err); } else{ if(post){ console.log(post); res.status(200).render('editDraft.hbs', {post: post}); } else{ routes._404(req,res); } } }); } else{ console.log('creating new draft'); res.status(200).render('editDraft.hbs'); } }); 

我正在使用Express和Mongoose。 视图引擎是把手。

谢谢阅读!

我认为地位200会把你抛弃。 尝试使用302,它应该工作。

 res.writeHead(302, { 'Location': '/draft?id='+id }); res.end();