Skip to content

Commit

Permalink
Use Object.create to setup request & response prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 23, 2017
1 parent 668f545 commit 12ff56e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ unreleased
* Remove usage of `res._headers` private field
- Improves compatibility with Node.js 8 nightly
* Skip routing when `req.url` is not set
* Use `Object.create` to setup request & response prototypes
* Use `statuses` instead of `http` module for status messages
* deps: debug@2.6.1
- Allow colors in workers
Expand Down
12 changes: 10 additions & 2 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ function createApplication() {
mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);

app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
// expose the prototype that will get set on requests
app.request = Object.create(req, {
app: { configurable: true, enumerable: true, writable: true, value: app }
})

// expose the prototype that will get set on responses
app.response = Object.create(res, {
app: { configurable: true, enumerable: true, writable: true, value: app }
})

app.init();
return app;
}
Expand Down
12 changes: 9 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ var proxyaddr = require('proxy-addr');

/**
* Request prototype.
* @public
*/

var req = exports = module.exports = {
__proto__: http.IncomingMessage.prototype
};
var req = Object.create(http.IncomingMessage.prototype)

/**
* Module exports.
* @public
*/

module.exports = req

/**
* Return request header.
Expand Down
12 changes: 9 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ var vary = require('vary');

/**
* Response prototype.
* @public
*/

var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
var res = Object.create(http.ServerResponse.prototype)

/**
* Module exports.
* @public
*/

module.exports = res

/**
* Module variables.
Expand Down

0 comments on commit 12ff56e

Please sign in to comment.