按照此处找到的答案https://stackoverflow.com/a/15362856(由于快速的中间件修订,我必须将const
添加到我的文件中。我似乎无法获得处理请求。我正在使用邮递员模拟邮件调用,当我发送POST时,服务器日志上没有任何内容显示,请求只是“继续旋转”。取消请求后,服务器日志会显示POST /login?username=foo&password=bar - - ms - -
这是我的server.js
文件:
var express = require('express')
, http = require('http')
, path = require('path')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
var app = express();
const flash = require('connect-flash');
const logger = require('morgan');
const cookieparser = require('cookie-parser');
const bodyparser = require('body-parser');
const session = require('express-session');
passport.use(new LocalStrategy(function(username, password, done) {
// insert your MongoDB check here. For now, just a simple hardcoded check.
if (username === 'foo' && password === 'bar')
{
done(null, { user: username });
}
else
{
done(null, false);
}
}));
passport.serializeUser(function(user, done) {
// please read the Passport documentation on how to implement this. We're now
// just serializing the entire 'user' object. It would be more sane to serialize
// just the unique user-id, so you can retrieve the user object from the database
// in .deserializeUser().
done(null, user);
});
passport.deserializeUser(function(user, done) {
// Again, read the documentation.
done(null, user);
});
app.use(logger('dev')); // log every request to the console
app.use(cookieparser); // read cookies (needed for auth)
app.use(bodyparser); // get information from html forms
app.use(session({ secret: 'nextoner' } )); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// route to authenticate the user
app.post('/login', passport.authenticate('local', {
successRedirect: '/accessed',
failureRedirect: '/access'
}));
app.listen(3012);
// app.listen(3012);
显然,我错过了一些东西,但经过两天的谷歌搜索,我似乎无法弄清楚它是什么。非常感谢帮助。
由于
答案 0 :(得分:0)
您似乎未正确初始化cookie-parser
和body-parser
。
试试这个:
app.use(cookieparser());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded());