Skip to content

Commit

Permalink
Add express.json and express.urlencoded to parse bodies
Browse files Browse the repository at this point in the history
closes #2211
  • Loading branch information
dougwilson committed Sep 28, 2017
1 parent 86f5df0 commit c0136d8
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Add `"json escape"` setting for `res.json` and `res.jsonp`
* Add `express.json` and `express.urlencoded` to parse bodies
* Improve error message when autoloading invalid view engine
* Improve error messages when non-function provided as middleware
* Skip `Buffer` encoding when not generating ETag for small response
Expand Down
3 changes: 1 addition & 2 deletions examples/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

var express = require('../..');
var bodyParser = require('body-parser');
var hash = require('pbkdf2-password')()
var path = require('path');
var session = require('express-session');
Expand All @@ -17,7 +16,7 @@ app.set('views', path.join(__dirname, 'views'));

// middleware

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: false }))
app.use(session({
resave: false, // don't save session if unmodified
saveUninitialized: false, // don't create session until something stored
Expand Down
3 changes: 1 addition & 2 deletions examples/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var express = require('../../');
var app = module.exports = express();
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// custom log format
if ('test' != process.env.NODE_ENV) app.use(logger(':method :url'));
Expand All @@ -18,7 +17,7 @@ if ('test' != process.env.NODE_ENV) app.use(logger(':method :url'));
app.use(cookieParser('my secret here'));

// parses x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: false }))

app.get('/', function(req, res){
if (req.cookies.remember) {
Expand Down
3 changes: 1 addition & 2 deletions examples/mvc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var express = require('../..');
var logger = require('morgan');
var path = require('path');
var session = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

var app = module.exports = express();
Expand Down Expand Up @@ -43,7 +42,7 @@ app.use(session({
}));

// parse request bodies (req.body)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true }))

// allow overriding methods in query (?_method=put)
app.use(methodOverride('_method'));
Expand Down
3 changes: 1 addition & 2 deletions examples/route-separation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var path = require('path');
var app = express();
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var site = require('./site');
var post = require('./post');
Expand All @@ -27,7 +26,7 @@ if (!module.parent) {

app.use(methodOverride('_method'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, 'public')));

// General
Expand Down
7 changes: 4 additions & 3 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Module dependencies.
*/

var bodyParser = require('body-parser')
var EventEmitter = require('events').EventEmitter;
var mixin = require('merge-descriptors');
var proto = require('./application');
Expand Down Expand Up @@ -74,16 +75,16 @@ exports.Router = Router;
* Expose middleware
*/

exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.static = require('serve-static');
exports.urlencoded = bodyParser.urlencoded

/**
* Replace removed middleware with an appropriate error message.
*/

[
'json',
'urlencoded',
;[
'bodyParser',
'compress',
'cookieSession',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"accepts": "~1.3.4",
"array-flatten": "1.1.1",
"body-parser": "1.18.2",
"content-disposition": "0.5.2",
"content-type": "~1.0.4",
"cookie": "0.3.1",
Expand Down Expand Up @@ -59,7 +60,6 @@
},
"devDependencies": {
"after": "0.8.2",
"body-parser": "1.18.2",
"cookie-parser": "~1.4.3",
"cookie-session": "1.3.2",
"ejs": "2.5.7",
Expand Down
3 changes: 1 addition & 2 deletions test/req.param.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

var express = require('../')
, request = require('supertest')
, bodyParser = require('body-parser')

describe('req', function(){
describe('.param(name, default)', function(){
Expand Down Expand Up @@ -34,7 +33,7 @@ describe('req', function(){
it('should check req.body', function(done){
var app = express();

app.use(bodyParser.json());
app.use(express.json())

app.use(function(req, res){
res.end(req.param('name'));
Expand Down

0 comments on commit c0136d8

Please sign in to comment.