Express body-parser request.body返回undefined

时间:2016-02-02 14:40:21

标签: node.js express body-parser

当我运行此应用程序并提交表单' console.log(request.body)'返回undefined。我是节点js的初学者。任何人都可以解释我做错了什么?

这是注册表 register.jade

doctype html
html
    head
        title.
            Fonebook
        link(rel='stylesheet', href='/assets/bootstrap/css/bootstrap.min.css')
        script(src='https://code.jquery.com/jquery-2.2.0.min.js')
        script(src='/assets/bootstrap/js/bootstrap.min.js')
    body
        div.container
            h3.
                The Fonebook
            form(method="POST",class="center",id="regForm",action='/doReg',enctype="multipart/form-data")
                div.panel.panel-info
                    div.panel-heading.
                        Registration
                    div.panel-body
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            label.
                                Profile Picture:
                            input(type='file',name='proPic')
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            label.
                                Email:
                            input.form-control(id="email", type="email", name="email", placeholder="Enter email ")
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            label.
                                Name:
                            input.form-control(id="name", type="text", name="name", placeholder="Enter name ")
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            label.
                                Password:
                            input.form-control(id="password", type="password", name="password")
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            a(href='login').
                                Already have an account?
                    div.panel-footer
                        button.btn.btn-info(type="submit").
                            Register

这是我的app.js文件

var express = require('express');
var app = express();

var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/test');

var uploadDir = __dirname + '/uploads';

var fs = require('fs');

var multer = require('multer');

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());


var conn = mongoose.connection;

app.set('views', './views');
app.set('view engine', 'jade');

app.use('/assets', express.static(__dirname + '/public'));

var port = process.env.PORT || 3000;

app.all('/', function (req, res) {
    res.render('login');

})

app.all('/register', function (req, res) {
    res.render('register');
})

app.all('/login', function (req, res) {
    res.render('login');
})

var storage = multer.diskStorage({
    destination: function (request, file, callback) {
        callback(null, uploadDir);
    },
    filename: function (request, file, callback) {
        //console.log(file);
        callback(null, Date.now() + '.jpg');
    }
});
var upload = multer({storage: storage}).single('proPic');

app.all('/doReg', function (request, response) {
    upload(request, response, function (err) {
        if (err) {
            console.log(err);
            return;
        }
        //console.log(request.file);
        response.end('Your File Uploaded');
    })
    console.log(request.body);
});

app.listen(port);

1 个答案:

答案 0 :(得分:0)

body-parser不支持多部分表单。

From their README

  

由于它们复杂且通常较大,因此不能处理多部分主体。对于多部分机构,您可能对以下模块感兴趣:

     

busboy和connect-busboy

     

multiparty和connect-multiparty

     

强大的

     

multer

看到您已经使用multer,您可以通过以下方式访问您的字段:

app.all('/doReg', upload, function (request, response) {
    console.log(request.body);
});