身体解析器的req.body继续返回undefined

时间:2016-06-06 11:25:08

标签: javascript node.js express

我相信标题的Content-Type设置为application/JSON。我的输入字段被包含在<form method="POST">中,其中包含type="submit"输入字段。为什么我会为身体获得undefined?什么是身体解析器的替代模块?

var express = require('express'),
    app = require('express')(),
    cookieSession = require('cookie-session'),
    ejs = require('ejs'),
    path = require('path'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    auth = require('./middleware/auth.js'),
    user = require('./models/user.js'),
    router = express.Router();

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', function (req, res) {
    //returns undefined
    console.log(req.body);
    user.create(req.session.username, req.session.email, req.session.password);
    res.render('./game/game.html', {});
});

app.get('/', function (req, res) {
    res.render('./index.html', {});
});

app.get('/game', function (req, res) {
   res.render('/views/index.html');
});

//expose router constructor
module.exports = router;

app.listen(3000);

0 个答案:

没有答案