express.js req.body总是空的

时间:2016-03-18 15:50:23

标签: javascript node.js express

我正在学习node.js和express.js,当我发帖时,我有一个空身。

我所做的是尝试从注册表中获取意见。 我的app.js是(相关部分):

var express = require('express');
var path = require('path');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');

var session = require('express-session');
var bodyParser = require('body-parser');
var multer = require('multer');


var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var upload = multer({ dest: 'uploads/' });

app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


// Handle Express session
app.use(session({
  secret:'secret',
  saveUnintialized: true,
  resave: true
}));

users.js是:

var express = require('express');
var router = express.Router();

// Add model for user.
// Note that variable has first uppercase, and model name is singular.
var User = require('../models/user');

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/register', function(req, res, next) {
  res.render('register', {
    'title': 'Register'
  });
});

router.get('/login', function(req, res, next) {
  res.render('login', {
    'title': 'Login'
  });
});

router.post('/register', function(req, res, next) {
  // These values come from the form.control name in register.jade
  console.log('Register clicked...');
  console.log('Body: ', req.body);

});

module.exports = router;

和register.jade是:

extends layout

block content
  h1 Register
  p Please register using the form below
  ul.errors
        if errors
                each error, i in errors
                    li.alert.alert-danger #{error.msg}
  form(method='post', action='/users/register', enctype='multipart/form-data')
      .form-group
          label Name
          input.form-control(name='name', type='text', placeholder='Enter Name')
      .form-group
          label Email
          input.form-control(name='email', type='text', placeholder='Enter Email')
      .form-group
          label Username
          input.form-control(name='username', type='text', placeholder='Enter Username')
      .form-group
          label Password
          input.form-control(name='password', type='password', placeholder='Enter password')
      .form-group
          label Confirm Password
          input.form-control(name='password2', type='password', placeholder='Confirm password')
      .form-group
          label Profile Image
          input.form-control(name='profileimage', type='file')
      input.btn.btn-default(name='submit', type='submit', value='Register')

怎么了?????我疯了: - (

由于

2 个答案:

答案 0 :(得分:3)

你的app.js中应该有app.use('/users', users);,确保> <{strong> <{strong> <{strong> <{strong> <{strong> ,因为正文解析器会填充app.use(bodyParser

如果您不需要文件上传或在users.js中使用多部分解析器,您还应该删除req.body

enctype='multipart/form-data'

答案 1 :(得分:0)

您可以在app.js中尝试:

var routes = require('./routes')
app.use(routes)