【发布时间】:2016-02-10 23:42:47
【问题描述】:
如何从发布请求重定向到不同的页面?
module.exports = function(app) {
app.post('/createStation', function(request, response){
response.redirect('/'); //This doesn't work, why and how to make this work
/*var stationDao = require('./server/stationDao.js');
stationDao.stationDao.createStation(request.body, function(status){
if(status.status == 'successful'){
response.redirect('/'); //This is what actually I wanted to do
}*/
});
});
};
也尝试使用 next(),
app.post('/createStation', [function(request, response, next){
var stationDao = require('./server/stationDao.js');
stationDao.stationDao.createStation(request.body, function(status){
if(status.status == 'successful'){
next();
}
});
}, function abc(request, response){
console.log('I can see this');
response.redirect('/'); //This doesn't work still
}]);
它实际上触发了 GET 请求,但页面不会重定向。
由于我是 node.js 的新手,任何对上述代码的建议都将不胜感激。
【问题讨论】:
-
你检查
status.status == 'successful'是否有效? -
您是否尝试过使用普通路由语法而不是使用
self.routeTable编写例程? -
没有。为什么你会认为 self.routeTable 会有问题?谢谢
-
您需要将 statusCode 设置为 302 并在响应 en.wikipedia.org/wiki/HTTP_302 的位置标头上传入您要重定向到的 url
-
好吧,我提出
self.routeTable的原因是我找不到任何关于它使用的官方文档。可能值得一试app.post('/createStation',function(req,res){})
标签: javascript node.js express npm dao