Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove body parser and various cleanup

  • Loading branch information...
commit 5c5e81fa31c5a55a06c334b7285a4f5c2dc28b75 1 parent c8801e6
@jonathanong jonathanong authored
View
22 Makefile
@@ -1,39 +1,19 @@
TESTS = test/*.js
REPORTER = dot
-DOX = ./node_modules/.bin/dox
-
-SRC = $(shell find lib/*.js lib/middleware/*.js)
-HTML = $(SRC:.js=.html)
test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--reporter $(REPORTER) \
$(TESTS)
-docs: $(HTML)
- @mv $(HTML) docs
-
test-cov: lib-cov
@CONNECT_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
lib-cov:
@jscoverage lib $@
-%.html: %.js
- $(DOX) < $< | node support/docs > $@
-
-docclean:
- rm -f $(HTML)
-
-site: docclean docs
- rm -fr /tmp/docs \
- && cp -fr docs /tmp/docs \
- && git checkout gh-pages \
- && cp -fr /tmp/docs/* . \
- && echo "done"
-
benchmark:
@./support/bench
-.PHONY: test-cov site docs test docclean benchmark
+.PHONY: test-cov test benchmark
View
4 Readme.md
@@ -32,6 +32,10 @@ If you would like to help maintain these middleware, please contact [@jongleberr
These middleware are officially supported by the Connect/Express team:
+ - [body-parser](https://github.com/expressjs/body-parser) - previous `bodyParser`, `json`, and `urlencoded`. You may also be interested in:
+ - [body](https://github.com/raynos/body)
+ - [co-body](https://github.com/visionmedia/co-body)
+ - [raw-body](https://github.com/stream-utils/raw-body)
- [compression](https://github.com/expressjs/compression) - previously `compress`
These middleware previously included with Connect are no longer supported by the Connect/Express team. Use one of these alternatives intead:
View
12 lib/connect.js
@@ -25,18 +25,6 @@ require('./patch');
exports = module.exports = createServer;
/**
- * Framework version.
- */
-
-exports.version = '2.7.11';
-
-/**
- * Expose mime module.
- */
-
-exports.mime = require('./middleware/static').mime;
-
-/**
* Expose the prototype.
*/
View
63 lib/middleware/bodyParser.js
@@ -1,63 +0,0 @@
-
-/*!
- * Connect - bodyParser
- * Copyright(c) 2010 Sencha Inc.
- * Copyright(c) 2011 TJ Holowaychuk
- * MIT Licensed
- */
-
-/**
- * Module dependencies.
- */
-
-var urlencoded = require('./urlencoded');
-var json = require('./json');
-
-/**
- * Body parser:
- *
- * Status: the multipart body parser will be removed in Connect 3.
- *
- * Parse request bodies, supports _application/json_,
- * _application/x-www-form-urlencoded_, and _multipart/form-data_.
- *
- * This is equivalent to:
- *
- * app.use(connect.json());
- * app.use(connect.urlencoded());
- * app.use(connect.multipart());
- *
- * Examples:
- *
- * connect()
- * .use(connect.bodyParser())
- * .use(function(req, res) {
- * res.end('viewing user ' + req.body.user.name);
- * });
- *
- * $ curl -d 'user[name]=tj' http://local/
- * $ curl -d '{"user":{"name":"tj"}}' -H "Content-Type: application/json" http://local/
- *
- * View [json](json.html), [urlencoded](urlencoded.html), and [multipart](multipart.html) for more info.
- *
- * If you wish to create your own body parser, you may be interested in:
- *
- * - [raw-body](https://github.com/stream-utils/raw-body)
- * - [body](https://github.com/raynos/body)
- *
- * @param {Object} options
- * @return {Function}
- * @api public
- */
-
-exports = module.exports = function bodyParser(options){
- var _urlencoded = urlencoded(options);
- var _json = json(options);
-
- return function bodyParser(req, res, next) {
- _json(req, res, function(err){
- if (err) return next(err);
- _urlencoded(req, res, next);
- });
- }
-};
View
87 lib/middleware/json.js
@@ -1,87 +0,0 @@
-
-/*!
- * Connect - json
- * Copyright(c) 2010 Sencha Inc.
- * Copyright(c) 2011 TJ Holowaychuk
- * MIT Licensed
- */
-
-/**
- * Module dependencies.
- */
-
-var utils = require('../utils');
-var getBody = require('raw-body');
-
-/**
- * JSON:
- *
- * Parse JSON request bodies, providing the
- * parsed object as `req.body`.
- *
- * Options:
- *
- * - `strict` when `false` anything `JSON.parse()` accepts will be parsed
- * - `reviver` used as the second "reviver" argument for JSON.parse
- * - `limit` byte limit [1mb]
- *
- * @param {Object} options
- * @return {Function}
- * @api public
- */
-
-exports = module.exports = function(options){
- options = options || {};
- var strict = options.strict !== false;
- var verify = typeof options.verify === 'function' && options.verify;
-
- return function json(req, res, next) {
- if (req._body) return next();
- req.body = req.body || {};
-
- if (!utils.hasBody(req)) return next();
-
- // check Content-Type
- if (!exports.regexp.test(utils.mime(req))) return next();
-
- // flag as parsed
- req._body = true;
-
- // parse
- getBody(req, {
- limit: options.limit || '1mb',
- length: req.headers['content-length'],
- encoding: 'utf8'
- }, function (err, buf) {
- if (err) return next(err);
-
- if (verify) {
- try {
- verify(req, res, buf)
- } catch (err) {
- if (!err.status) err.status = 403;
- return next(err);
- }
- }
-
- var first = buf.trim()[0];
-
- if (0 == buf.length) {
- return next(utils.error(400, 'invalid json, empty body'));
- }
-
- if (strict && '{' != first && '[' != first) return next(utils.error(400, 'invalid json'));
- try {
- req.body = JSON.parse(buf, options.reviver);
- } catch (err){
- err.body = buf;
- err.status = 400;
- return next(err);
- }
- next();
- })
- };
-};
-
-exports.regexp = /^application\/([\w!#\$%&\*`\-\.\^~]*\+)?json$/i;
-
View
9 lib/middleware/static.js
@@ -85,12 +85,3 @@ exports = module.exports = function(root, options){
.pipe(res);
};
};
-
-/**
- * Expose mime module.
- *
- * If you wish to extend the mime table use this
- * reference to the "mime" module in the npm registry.
- */
-
-exports.mime = send.mime;
View
76 lib/middleware/urlencoded.js
@@ -1,76 +0,0 @@
-/*!
- * Connect - urlencoded
- * Copyright(c) 2010 Sencha Inc.
- * Copyright(c) 2011 TJ Holowaychuk
- * MIT Licensed
- */
-
-/**
- * Module dependencies.
- */
-
-var utils = require('../utils');
-var getBody = require('raw-body');
-var qs = require('qs');
-
-/**
- * Urlencoded:
- *
- * Parse x-ww-form-urlencoded request bodies,
- * providing the parsed object as `req.body` using
- * [qs](https://github.com/visionmedia/node-querystring).
- *
- * Options:
- *
- * - `limit` byte limit [1mb]
- *
- * @param {Object} options
- * @return {Function}
- * @api public
- */
-
-exports = module.exports = function(options){
- options = options || {};
- var verify = typeof options.verify === 'function' && options.verify;
-
- return function urlencoded(req, res, next) {
- if (req._body) return next();
- req.body = req.body || {};
-
- if (!utils.hasBody(req)) return next();
-
- // check Content-Type
- if ('application/x-www-form-urlencoded' != utils.mime(req)) return next();
-
- // flag as parsed
- req._body = true;
-
- // parse
- getBody(req, {
- limit: options.limit || '1mb',
- length: req.headers['content-length'],
- encoding: 'utf8'
- }, function (err, buf) {
- if (err) return next(err);
-
- if (verify) {
- try {
- verify(req, res, buf)
- } catch (err) {
- if (!err.status) err.status = 403;
- return next(err);
- }
- }
-
- try {
- req.body = buf.length
- ? qs.parse(buf)
- : {};
- } catch (err){
- err.body = buf;
- return next(err);
- }
- next();
- })
- }
-};
View
29 lib/utils.js
@@ -17,35 +17,6 @@ var http = require('http')
, signature = require('cookie-signature');
/**
- * Return `true` if the request has a body, otherwise return `false`.
- *
- * @param {IncomingMessage} req
- * @return {Boolean}
- * @api private
- */
-
-exports.hasBody = function(req) {
- var encoding = 'transfer-encoding' in req.headers;
- var length = 'content-length' in req.headers && req.headers['content-length'] !== '0';
- return encoding || length;
-};
-
-/**
- * Extract the mime type from the given request's
- * _Content-Type_ header.
- *
- * @param {IncomingMessage} req
- * @return {String}
- * @api private
- */
-
-exports.mime = function(req) {
- var str = req.headers['content-type'] || ''
- , i = str.indexOf(';');
- return ~i ? str.slice(0, i) : str;
-};
-
-/**
* Generate an `Error` from the given status `code`
* and optional `msg`.
*
View
7 package.json
@@ -23,14 +23,11 @@
"uid2": "0.0.3",
"debug": ">= 0.7.3 < 1",
"methods": "0.1.0",
- "raw-body": "1.1.2",
"negotiator": "0.3.0"
},
"devDependencies": {
"should": ">= 2.0.2 < 3",
- "mocha": ">= 1.13.0 < 2",
- "jade": ">= 0.35.0 < 1",
- "dox": ">= 0.4.4 < 1"
+ "mocha": ">= 1.13.0 < 2"
},
"licenses": [
{
@@ -40,7 +37,7 @@
],
"main": "index",
"engines": {
- "node": ">= 0.8.0"
+ "node": ">= 0.10.0"
},
"scripts": {
"test": "make"
View
44 test/bodyParser.js
@@ -1,44 +0,0 @@
-
-var connect = require('..');
-var assert = require('assert');
-
-var app = connect();
-
-app.use(connect.bodyParser());
-
-app.use(function(req, res){
- res.end(JSON.stringify(req.body));
-});
-
-describe('connect.bodyParser()', function(){
- it('should default to {}', function(done){
- app.request()
- .post('/')
- .end(function(res){
- res.body.should.equal('{}');
- done();
- })
- })
-
- it('should parse JSON', function(done){
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('{"user":"tobi"}')
- .end(function(res){
- res.body.should.equal('{"user":"tobi"}');
- done();
- });
- })
-
- it('should parse x-www-form-urlencoded', function(done){
- app.request()
- .post('/')
- .set('Content-Type', 'application/x-www-form-urlencoded')
- .write('user=tobi')
- .end(function(res){
- res.body.should.equal('{"user":"tobi"}');
- done();
- });
- })
-})
View
3  test/csrf.js
@@ -6,7 +6,6 @@ describe('csrf', function(){
app.use(connect.cookieParser())
app.use(connect.session({ secret: 'greg' }));
- app.use(connect.bodyParser());
app.use(connect.csrf());
app.use(function(req, res){
res.end(req.csrfToken() || 'none');
@@ -33,7 +32,6 @@ describe('csrf', function(){
app.use(connect.cookieParser());
app.use(connect.session({ secret: 'greg' }));
- app.use(connect.bodyParser());
app.use(connect.csrf());
app.use(function(req, res){
res.end(req.csrfToken() || 'none');
@@ -58,7 +56,6 @@ describe('csrf', function(){
app.use(connect.cookieParser());
app.use(connect.session({ secret: 'greg' }));
- app.use(connect.bodyParser());
app.use(connect.csrf());
app.use(function(req, res){
res.end(req.csrfToken() || 'none');
View
26 test/exports.js
@@ -1,26 +0,0 @@
-
-var connect = require('../');
-
-describe('exports', function(){
- describe('.version', function(){
- it('should be a string', function(){
- connect.version.should.be.a.String;
- })
- })
-
- describe('.middleware', function(){
- it('should lazy-load middleware', function(){
- connect.middleware.cookieParser.should.be.a.Function;
- connect.middleware.bodyParser.should.be.a.Function;
- connect.middleware.static.should.be.a.Function;
- })
- })
-
- describe('.NAME', function(){
- it('should lazy-load middleware', function(){
- connect.cookieParser.should.be.a.Function;
- connect.bodyParser.should.be.a.Function;
- connect.static.should.be.a.Function;
- })
- })
-})
View
279 test/json.js
@@ -1,279 +0,0 @@
-
-var connect = require('../')
- , should = require('./shared');
-
-var app = connect();
-
-app.use(connect.json({ limit: '1mb' }));
-
-app.use(function(req, res){
- res.end(JSON.stringify(req.body));
-});
-
-app.use(function(err, req, res, next){
- res.statusCode = err.status;
- res.end(err.message);
-});
-
-describe('connect.json()', function(){
- should['default request body'](app);
- should['limit body to']('1mb', 'application/json', app);
-
- it('should parse JSON', function(done){
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('{"user":"tobi"}')
- .end(function(res){
- res.body.should.equal('{"user":"tobi"}');
- done();
- });
- })
-
- it('should fail gracefully', function(done){
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('{"user"')
- .end(function(res){
- res.body.should.equal('Unexpected end of input');
- done();
- });
- })
-
- it('should handle Content-Length: 0', function(done){
- var app = connect();
- app.use(connect.json());
-
- app.use(function(req, res){
- res.end(Object.keys(req.body).length ? '' : 'empty');
- });
-
- app.request()
- .get('/')
- .set('Content-Type', 'application/json')
- .set('Content-Length', '0')
- .end(function(res){
- res.should.have.status(200);
- res.body.should.equal('empty');
- done();
- });
- })
-
- it('should 400 on malformed JSON', function(done){
- var app = connect();
- app.use(connect.json());
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('{"foo')
- .expect(400, done);
- })
-
- it('should 400 when no body is given', function(done){
- var app = connect();
- app.use(connect.json());
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .end(function(res) {
- res.should.have.status(400);
- res.body.should.include("invalid json, empty body");
- done();
- })
- })
-
- it('should support all http methods', function(done){
- var app = connect();
- app.use(connect.json());
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .get('/')
- .set('Content-Type', 'application/json')
- .set('Content-Length', '["foo"]'.length)
- .write('["foo"]')
- .expect('["foo"]', done);
- })
-
- describe('when strict is false', function(){
- it('should parse primitives', function(done){
- var app = connect();
- app.use(connect.json({ strict: false }));
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('true')
- .expect('true', done);
- })
- })
-
- describe('when strict is true', function(){
- it('should not parse primitives', function(done){
- var app = connect();
- app.use(connect.json({ strict: true }));
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('true')
- .end(function(res){
- res.should.have.status(400);
- res.body.should.include('invalid json');
- done();
- });
- })
-
- it('should allow leading whitespaces in JSON', function(done){
- var app = connect();
- app.use(connect.json({ strict: true }));
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write(' { "user": "tobi" }')
- .end(function(res){
- res.should.have.status(200);
- res.body.should.include('{"user":"tobi"}');
- done();
- });
- })
- })
-
- describe('by default', function(){
- it('should 400 on primitives', function(done){
- var app = connect();
- app.use(connect.json());
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('true')
- .expect(400, done);
- })
- })
-
- it('should support utf-8', function(done){
- var app = connect();
-
- app.use(connect.json());
-
- app.use(function(req, res, next){
- res.end(req.body.name);
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json; charset=utf-8')
- .write('{"name":"论"}')
- .expect('', done);
- })
-
- it('should support {"test":"å"}', function (done) {
- // https://github.com/visionmedia/express/issues/1816
-
- var app = connect();
- app.use(connect.json());
- app.use(function(req, res, next){
- res.end(req.body.test);
- })
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json; charset=utf-8')
- .set('Content-Length', '13')
- .write('{"test":"å"}')
- .expect('å', done);
- })
-
- describe('the default json mime type regular expression', function() {
- var mimeRegExp = connect.json.regexp;
- it('should support the basic JSON mime type', function(){
- mimeRegExp.test('application/json').should.eql(true);
- })
-
- it('should not match incorrect mime type', function(){
- mimeRegExp.test('zapplication/json').should.eql(false);
- })
-
- it('should be case insensitive', function(){
- mimeRegExp.test('Application/JSON').should.eql(true);
- })
-
- it('should support suffix notation', function(){
- mimeRegExp.test('application/vnd.organization.prog-type.org+json').should.eql(true);
- })
-
- it('should support specific special characters on mime subtype', function(){
- mimeRegExp.test('application/vnd.organization.special_!#$%*`-^~.org+json').should.eql(true);
- })
-
- it('should not support other special characters on mime subtype', function(){
- mimeRegExp.test('application/vnd.organization.special_()<>@,;:\\"/[]?={} \t.org+json').should.eql(false);
- })
-
- it('should not match malformed mime subtype suffix', function(){
- mimeRegExp.test('application/vnd.test.org+json+xml').should.eql(false);
- })
- });
-
- if (!connect.utils.brokenPause) {
- it('should parse JSON with limit and after next tick', function(done){
- var app = connect();
-
- app.use(function(req, res, next) {
- setTimeout(next, 10);
- });
-
- app.use(connect.json({ limit: '1mb' }));
-
- app.use(function(req, res){
- res.end(JSON.stringify(req.body));
- });
-
- app.use(function(err, req, res, next){
- res.statusCode = err.status;
- res.end(err.message);
- });
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('{"user":"tobi"}')
- .end(function(res){
- res.body.should.equal('{"user":"tobi"}');
- done();
- });
- })
- }
-})
View
13 test/methodOverride.js
@@ -3,7 +3,6 @@ var connect = require('../');
var app = connect();
-app.use(connect.bodyParser());
app.use(connect.methodOverride());
app.use(function(req, res){
@@ -17,19 +16,11 @@ describe('connect.methodOverride()', function(){
.expect('GET', done);
})
- it('should support req.body._method', function(done){
- app.request()
- .post('/')
- .set('Content-Type', 'application/x-www-form-urlencoded')
- .write('_method=DELETE')
- .expect('DELETE', done);
- })
-
it('should be case in-sensitive', function(done){
app.request()
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
- .write('_method=delete')
+ .set('X-HTTP-Method-Override', 'DELETE')
.expect('DELETE', done);
})
@@ -37,7 +28,7 @@ describe('connect.methodOverride()', function(){
app.request()
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
- .write('_method=<whatever>')
+ .set('X-HTTP-Method-Override', 'POST')
.expect('POST', done);
})
})
View
23 test/server.js
@@ -20,29 +20,6 @@ describe('app', function(){
.expect('http://example.com/foo', done);
})
- it('should allow old-style constructor middleware', function(done){
- var app = connect(
- connect.json()
- , connect.urlencoded()
- , function(req, res){ res.end(JSON.stringify(req.body)) });
-
- app.stack.should.have.length(3);
-
- app.request()
- .post('/')
- .set('Content-Type', 'application/json')
- .write('{"foo":"bar"}')
- .expect('{"foo":"bar"}', done);
- })
-
- it('should allow old-style .createServer()', function(){
- var app = connect.createServer(
- connect.json()
- , connect.urlencoded());
-
- app.stack.should.have.length(2);
- })
-
it('should work as middlware', function(done){
var http = require('http');
View
26 test/shared/index.js
@@ -1,26 +0,0 @@
-
-var bytes = require('bytes');
-
-exports['default request body'] = function(app){
- it('should default to {}', function(done){
- app.request()
- .post('/')
- .end(function(res){
- res.body.should.equal('{}');
- done();
- })
- })
-};
-
-exports['limit body to'] = function(size, type, app){
- it('should accept a limit option', function(done){
- app.request()
- .post('/')
- .set('Content-Length', bytes(size) + 1)
- .set('Content-Type', type)
- .end(function(res){
- res.should.have.status(413);
- done();
- })
- })
-}
View
39 test/urlencoded.js
@@ -1,39 +0,0 @@
-
-var connect = require('../')
- , should = require('./shared');
-
-var app = connect();
-
-app.use(connect.urlencoded({ limit: '1mb' }));
-
-app.use(function(req, res){
- res.end(JSON.stringify(req.body));
-});
-
-describe('connect.urlencoded()', function(){
- should['default request body'](app);
- should['limit body to']('1mb', 'application/x-www-form-urlencoded', app);
-
- it('should support all http methods', function(done){
- app.request()
- .get('/')
- .set('Content-Type', 'application/x-www-form-urlencoded')
- .set('Content-Length', 'user=tobi'.length)
- .write('user=tobi')
- .end(function(res){
- res.body.should.equal('{"user":"tobi"}');
- done();
- });
- })
-
- it('should parse x-www-form-urlencoded', function(done){
- app.request()
- .post('/')
- .set('Content-Type', 'application/x-www-form-urlencoded')
- .write('user=tobi')
- .end(function(res){
- res.body.should.equal('{"user":"tobi"}');
- done();
- });
- })
-})
View
32 test/utils.js
@@ -1,32 +0,0 @@
-
-var connect = require('../')
- , utils = connect.utils;
-
-describe('utils.parseCacheControl(str)', function(){
- it('should parse Cache-Control', function(){
- var parse = utils.parseCacheControl;
- parse('no-cache').should.eql({ 'no-cache': true });
- parse('no-store').should.eql({ 'no-store': true });
- parse('no-transform').should.eql({ 'no-transform': true });
- parse('only-if-cached').should.eql({ 'only-if-cached': true });
- parse('max-age=0').should.eql({ 'max-age': 0 });
- parse('max-age=60').should.eql({ 'max-age': 60 });
- parse('max-stale=60').should.eql({ 'max-stale': 60 });
- parse('min-fresh=60').should.eql({ 'min-fresh': 60 });
- parse('public, max-age=60').should.eql({ 'public': true, 'max-age': 60 });
- parse('must-revalidate, max-age=60').should.eql({ 'must-revalidate': true, 'max-age': 60 });
- })
-})
-
-describe('utils.mime(req)', function(){
- it('should return the mime-type from Content-Type', function(){
- utils.mime({ headers: { 'content-type': 'text/html; charset=utf8' }})
- .should.equal('text/html');
-
- utils.mime({ headers: { 'content-type': 'text/html; charset=utf8' }})
- .should.equal('text/html');
-
- utils.mime({ headers: { 'content-type': 'text/html' }})
- .should.equal('text/html');
- })
-})
Please sign in to comment.
Something went wrong with that request. Please try again.