#415 Missing body for POST request in middleware (#576)

* Missing postbody in middleware

Added body parser to server defaults.

* Test name change

Capitol letter is now lower case

* Missing postbody in middleware

Added body parser to server defaults.

* Test name change

Capitol letter is now lower case

* Added postbody as option to defaults

* Missing postbody in middleware

Added body parser to server defaults.

* Test name change

Capitol letter is now lower case

* Missing postbody in middleware

Added body parser to server defaults.

* Added postbody as option to defaults

* Setting bodyParser as enabled by default for cli
This commit is contained in:
JHotterbeekx
2017-07-14 02:41:55 +02:00
committed by typicode
parent b9f63b9d77
commit 6ca65ef982
4 changed files with 321 additions and 298 deletions

View File

@ -56,7 +56,8 @@ function createApp(source, object, routes, middlewares, argv) {
logger: !argv.quiet, logger: !argv.quiet,
readOnly: argv.readOnly, readOnly: argv.readOnly,
noCors: argv.noCors, noCors: argv.noCors,
noGzip: argv.noGzip noGzip: argv.noGzip,
bodyParser: true
} }
if (argv.static) { if (argv.static) {

View File

@ -6,6 +6,7 @@ const cors = require('cors')
const compression = require('compression') const compression = require('compression')
const errorhandler = require('errorhandler') const errorhandler = require('errorhandler')
const objectAssign = require('object-assign') const objectAssign = require('object-assign')
const bodyParser = require('./body-parser')
module.exports = function(opts) { module.exports = function(opts) {
const userDir = path.join(process.cwd(), 'public') const userDir = path.join(process.cwd(), 'public')
@ -64,5 +65,10 @@ module.exports = function(opts) {
}) })
} }
// Add middlewares
if (opts.bodyParser) {
arr.push(bodyParser)
}
return arr return arr
} }

View File

@ -0,0 +1,4 @@
module.exports = function(req, res, next) {
res.header('name', req.body.name)
next()
}

View File

@ -14,7 +14,8 @@ let PORT = 3100
const middlewareFiles = { const middlewareFiles = {
en: './fixtures/middlewares/en.js', en: './fixtures/middlewares/en.js',
jp: './fixtures/middlewares/jp.js' jp: './fixtures/middlewares/jp.js',
postbody: './fixtures/middlewares/postbody.js'
} }
const bin = path.join(__dirname, '../../lib/cli/bin') const bin = path.join(__dirname, '../../lib/cli/bin')
@ -162,6 +163,17 @@ describe('cli', () => {
}) })
}) })
describe('db.json -m postbody-middleware.js', () => {
beforeEach(done => {
child = cli([dbFile, '-m', middlewareFiles.postbody])
serverReady(PORT, done)
})
it('should have post body in middleware', done => {
request.post('/posts').send({ name: 'test' }).expect('name', 'test', done)
})
})
describe('db.json -d 1000', () => { describe('db.json -d 1000', () => {
beforeEach(done => { beforeEach(done => {
child = cli([dbFile, '-d', 1000]) child = cli([dbFile, '-d', 1000])