我正在尝试用户身份验证(以及一般的MEAN),我遇到了一个奇怪的400错误请求错误。
我有一个简单的登录表单,发布到API端点,服务器始终显示400错误请求。我已经检查了crhome中的开发控制台,我似乎无法找到帖子数据。此外,从Stack Overflow上的许多其他问题中分离我的问题是我没有使用Ajax,也没有看到Angular或Node Console中的错误消息...只有节点和Chrome开发控制台中的状态代码
我的代码在下面,提前谢谢!
这是我的表单代码:
<form class="form-inline navbar-form navbar-right" action="/todo" method="post">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="Enter email">
</div>
<div class="form-group">
<label class="sr-only" for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="loginData.rememberme"> Remember me
</label>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
现在是我的中间件代码。
var express = require('express');
var app = express(); // create our app w/ express
var router = express.Router();
var session = require('cookie-session');
var mongoose = require('mongoose'); // mongoose for mongodb
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
var flash = require('connect-flash');
var path = require('path');
var bcrypt = require('bcrypt-nodejs');
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
// configuration =================
mongoose.connect('mongodb://localhost/todoApp'); // connect to mongoDB
app.use(express.static(__dirname + '/')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({
'extended': 'true'
})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
//app.use(bodyParser.json({
//type: 'application/vnd.api+json'
//})); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use(session({
secret: 'Noble64'
}));
//app.use(express.session({ secret: 'noble64' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, user);
});
}));
app.post('/todo',
passport.authenticate('local-login'),
function(req, email, password, done) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, user);
});
res.json(req.user);
});
答案 0 :(得分:0)
您确定要正确连接数据库吗?连接到数据库时,您没有提供端口。示例:
mongoose.connect('mongodb://localhost:27017/todoApp');