2016-03-31 7 views
3

私は実際にExpressでnode.jsに非常に小さなアプリケーションを持っていますが、req.bodyにアクセスすることはできません。ミドルウェア内のreq.bodyにアクセスできない

これは私のapp.jsコードです:

var express = require('express'), 
    middleware = require('./middleware'), 
    mysql = require('mysql'), 
    app = express(); 

app.use(middleware.authenticate); 
app.use(middleware.getScope); 

app.listen(3000); 
module.exports = app; 

そしてミドルウェアを使用してファイル:すべてのケースで

var bcrypt = require('bcrypt'), 
    mysql = require('mysql'); 

function authenticate(req, res, next){ 
    console.log(req.body); 
    next(); 
} 

function getScope(req, res, next){ 
    console.log(req.body); 
    next(); 
} 

module.exports.authenticate = authenticate; 
module.exports.getScope = getScope; 

はreq.body undefinedです。

私はデータを送信していますx-www-form-urlencodedプロトコルで郵便配達員、この場合は必須です。

enter image description here

感謝!!あなたが表現するbody-parserを追加する必要が

+0

をあなたは[ '体を必要とします-parser'](https://github.com/expressjs/body-parser)モジュールをインストールします。 –

答えて

5

var bodyParser = require('body-parser'); 
app.use(bodyParser.json()); // for parsing application/json 
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded 

チェック要求をここhttp://expressjs.com/en/api.html

、おそらくPOSTルートはあまりにも助けることができる:

app.post('/', function (req, res, next) { 
    console.log(req.body); 
    res.json(req.body); 
}); 
関連する問題