Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: strongloop/express
...
head fork: coopernurse/express
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 53 additions and 1 deletion.
  1. +31 −0 lib/express/request.js
  2. +22 −1 test/request.test.js
View
31 lib/express/request.js
@@ -136,6 +136,37 @@ http.IncomingMessage.prototype.param = function(name){
};
/**
+ * Gets/Sets a request local variable
+ *
+ * To set a value:
+ * req.local('userId', 'abc123');
+ *
+ * To get a value:
+ * var userId = req.local('userId');
+ *
+ * Useful when chaining routes with next()
+ * The first route handler can set a variable on
+ * the request, which can be accessed by the next
+ * route handler. Common use case is authentication
+ * filters, which can set a 'user' object that can
+ * be accessed downstream on the request.
+ */
+
+http.IncomingMessage.prototype.local = function(name, value) {
+ if (!this.locals) {
+ this.locals = {};
+ }
+
+ if (value) {
+ // set case
+ this.locals[name] = value;
+ }
+ else {
+ return this.locals[name];
+ }
+};
+
+/**
* Queue flash `msg` of the given `type`.
*
* Examples:
View
23 test/request.test.js
@@ -153,5 +153,26 @@ module.exports = {
assert.response(app,
{ url: '/' },
{ body: 'ok' });
+ },
+
+ 'test #local()': function(assert) {
+ var app = express.createServer();
+
+ app.get('/', function(req, res) {
+ assert.eql(undefined, req.local('userId'));
+
+ req.local('userId', 'abc');
+ assert.eql('abc', req.local('userId'));
+
+ req.local('userId', ['a','b']);
+ assert.eql(['a','b'], req.local('userId'));
+
+ res.send('ok');
+ });
+
+ assert.response(app,
+ { url: '/' },
+ { body: 'ok' });
}
-};
+
+};

No commit comments for this range

Something went wrong with that request. Please try again.