Javascript Node.js URL后重写

Javascript Node.js URL后重写,javascript,node.js,url,url-rewriting,Javascript,Node.js,Url,Url Rewriting,我在js文件中有这个函数: exports.authentication = function(req, res) { // .. // user validation // .. res.redirect('/login'); }; 如何将所有POST请求重写到不同的路径,如/admin/XY,即在我的示例中是/admin/login而不是/login?我现在可以硬编码了,但我希望这样。redirect()会自动为所有项目文件中的所有POST请求追加此代码。使

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

exports.authentication = function(req, res) {
   // ..
   // user validation
   // ..

   res.redirect('/login');   
};

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

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

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

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

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

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

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

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

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

使用此代码,您可以在验证后使用相同的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
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
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
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    
}));

您正在使用哪个编辑器?对不起,先生,我不想使用文本编辑器进行替换操作。是否可以将其编码为Express()?我已经在对GET请求使用url_rewrite,如下所示:
exports.temp_rewrite=function(){return function(req,res,next){req.url='/admin'+req.url;next();}}
但现在不知道如何对POST请求执行此操作。重定向方法支持相对路径:也许这已经解决了您的问题?@masch,你是对的!我使用的是“路径名相对”重定向,而不是“装载相对”重定向。在我的例子中,重定向应该是:res.redirect('login');非常感谢。您正在使用哪个编辑器?对不起,先生,我不想使用文本编辑器进行替换操作。是否可以将其编码为Express()?我已经在对GET请求使用url_rewrite,如下所示:
exports.temp_rewrite=function(){return function(req,res,next){req.url='/admin'+req.url;next();}}
但现在不知道如何对POST请求执行此操作。重定向方法支持相对路径:也许这已经解决了您的问题?@masch,你是对的!我使用的是“路径名相对”重定向,而不是“装载相对”重定向。在我的例子中,重定向应该是:res.redirect('login');非常感谢。您正在使用哪个编辑器?对不起,先生,我不想使用文本编辑器进行替换操作。是否可以将其编码为Express()?我已经在对GET请求使用url_rewrite,如下所示:
exports.temp_rewrite=function(){return function(req,res,next){req.url='/admin'+req.url;next();}}
但现在不知道如何对POST请求执行此操作。重定向方法支持相对路径:也许这已经解决了您的问题?@masch,你是对的!我使用的是“路径名相对”重定向,而不是“装载相对”重定向。在我的例子中,重定向应该是:res.redirect('login');非常感谢。您正在使用哪个编辑器?对不起,先生,我不想使用文本编辑器进行替换操作。是否可以将其编码为Express()?我已经在对GET请求使用url_rewrite,如下所示:
exports.temp_rewrite=function(){return function(req,res,next){req.url='/admin'+req.url;next();}}
但现在不知道如何对POST请求执行此操作。重定向方法支持相对路径:也许这已经解决了您的问题?@masch,你是对的!我使用的是“路径名相对”重定向,而不是“装载相对”重定向。在我的例子中,重定向应该是:res.redirect('login');非常感谢。