Node.js URL POST rewrite

Node.js URL POST rewrite

本文关键字:rewrite POST URL js Node      更新时间:2023-09-26

我在js文件中有这个函数:

exports.authentication = function(req, res) {
   // ..
   // user validation
   // ..
   res.redirect('/login');   
};

我如何重写所有POST请求到不同的路径,如/admin/XY,即/admin/login而不是/login在我的例子中?我现在可以硬编码这个,但我希望.redirect()将自动附加到所有项目文件中的所有POST请求。

使用此代码,您可以在验证后使用相同的url failureRedirect:

app.post('/login', passport.authenticate('local-login', {    
            successRedirect : '/profile', // redirect to the secure profile section    
            failureRedirect : '/login', // redirect back to the signup page if there is an error    
            failureFlash : true // allow flash messages    
}));

使用此代码重定向url中的所有post请求。

app.post('^*$', function(req, res) {
  res.redirect('/postRedirectUrl');
});