简而言之,我有一个登录页面,我的POST请求在Express 4中变为GET. 这是我的登录页面的业务部分: form id='loginForm' action='/login' method='post' table tr tdinput type='text' name='username' //td /tr tr tdinput ty
这是我的登录页面的业务部分:
<form id='loginForm' action='/login' method='post'>
<table>
<tr>
<td><input type='text' name='username' /></td>
</tr>
<tr>
<td><input type='text' name='password' /></td>
</tr>
<tr>
<td><button type='submit'>Submit</button></td>
</tr>
</table>
</form>
这是我设置的路线:
// Express 4
var router = express.Router();
router.get('/login', function(req, res) {
res.render('./login.hbs', { message: req.flash('loginMessage') });
});
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/welcome',
failureRedirect: '/login',
failureFlash : true
}));
passport.use('local-login',
new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
passReqToCallback : true
},
function(req, email, password, next) {
User.findOne({ 'authLocal.email' : email }, function(err, user) {
console.log('Inside local-login strategy with findOne A');
[snip]
我在res.render上为GET / login和本地登录策略中的findOne()调用设置了一个断点.当我单击“提交”按钮时,捕获事物的断点位于router.get()代码中.
在调试器中,req.method是GET.
在浏览器(Chrome)中,我被告知我正在对返回302的/登录进行POST.还有一个待处理的GET /登录,带有200个代码.这两个代码(302,200)是调试器在router.get()中停止的时间.即使清除浏览器缓存也无济于事.
有人能告诉我为什么我的POST请求没有被尊重?
简单的答案POST /登录变为GET /登录302的原因是护照模块完成的重定向.
请参阅以下代码中的failureRedirect:
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/welcome',
failureRedirect: '/login',
failureFlash : true
}));
当提供的凭据无效或服务器因任何原因无法进行身份验证时,可能会发生此问题.
快乐帮助!
