我试图发布到快递4中的路线,但是当我尝试记录身体时我得到了未定义的消息,这是我的配置:
/server.js
/* main module entry point */
'use strict';
var express = require( './server/node_modules/express' );
var bodyParser = require( './server/node_modules/body-parser' );
var env = require( './server/env' );
var app = module.exports = express();
app.use( require( './server/modules/auth' ) );
app.use( express.static( __dirname + '/client/' ) );
app.use( bodyParser.urlencoded( {
extended: true
} ) );
app.use( bodyParser.json() );
app.get( '/*', function ( require, response ) {
response.sendFile( __dirname + '/client/index.html' );
} );
app.listen( env.port, function () {
console.log( 'Listening to port ' + env.port );
} );
/server/modules/auth/index.js
module.exports = (function () {
var express = require('express'),
router = express.Router(),
signin = require('./signin.controller');
router.route( '/signin' )
.get( signin.get )
.post( signin.post );
return router;
})();
/server/modules/auth/sigin.controller.js
module.exports = (function () {
models = require( '../../database/models' );
function get( request, response ) {
models.users.findAll().then( function ( users ) {
response.json( users );
} );
console.log( 'Sign In' );
}
function post( request, response ) {
console.log( 'Sign In ' + request.body );//Returns "Sign In Undefined"
response.send('Signed In'+ request.body);
}
return {
get: get,
post: post
}
})();
我正在使用的标题是:
Content-Type:application / json
答案 0 :(得分:2)
订单很重要。试试这个:
/* main module entry point */
'use strict';
var express = require( './server/node_modules/express' );
var bodyParser = require( './server/node_modules/body-parser' );
var env = require( './server/env' );
var app = module.exports = express();
app.use( express.static( __dirname + '/client/' ) );
app.use( bodyParser.urlencoded( {
extended: true
} ) );
app.use( bodyParser.json() );
app.use( require( './server/modules/auth' ) );
app.get( '/*', function ( require, response ) {
response.sendFile( __dirname + '/client/index.html' );
} );
app.listen( env.port, function () {
console.log( 'Listening to port ' + env.port );
} );
基本上你告诉表示不要在运行auth.js中的中间件之后解析正文内容。
答案 1 :(得分:0)
从4.16开始,可以随时使用express.json()
代替body-parse
,更多细节:express.json vs bodyParser.json。