【问题标题】:Form Post results in bad request 400表单发布导致错误请求 400
【发布时间】:2015-05-11 15:10:55
【问题描述】:

我正在尝试用户身份验证(以及一般的 MEAN),但我遇到了一个奇怪的 400 错误请求错误。

我有一个发布到 API 端点的简单登录表单,服务器总是显示 400 错误请求。我检查了crhome中的开发控制台,似乎找不到post数据。此外,将我的问题与 Stack Overflow 上的许多其他问题区分开来的是我没有使用 Ajax,也没有看到来自 Angular 或节点控制台的错误消息......只有节点和 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);

      });

【问题讨论】:

    标签: angularjs node.js express


    【解决方案1】:

    您确定您正确连接到数据库吗?您在连接到数据库时没有提供端口。示例:

    mongoose.connect('mongodb://localhost:27017/todoApp');
    

    【讨论】:

    • 在我添加登录表单之前,它连接到数据库很好,但我可以试一试。
    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2020-05-08
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多