unique data pre user/sessions, as TJ said, this is what is req.session for. req.locals is exactly the same as req.session, but only selfmade. this is why you've never seen someone doing it, because req.session is there.
to your question: you create 2 separate applications with separated app.locals, and then you mount the app in route.js into your main app.
you have different options to solve that. first one is not to mount separate app:
./app.js
var express = require('express');
var app = express();
app.locals.foo = 'some things'; // i want to globalize foo.
var route = require('./lib.route');
route(app);
./lib/route.js
module.exports = function(app){
app.get('/route', function (req, res, next) {
assert.equal(app.locals.foo, 'some things'); // this will throw
})
}
this way you have one app with several routes
./app.js
var express = require('express');
var app = express();
app.locals.foo = 'some things'; // i want to globalize foo.
var route = require('./lib.route');
app.use(route);
route.locals = app.locals
./lib/route.js
var express = require('express');
var app = module.exports = express();
app.get('/route', function (req, res, next) {
assert.equal(app.locals.foo, 'some things'); // this will throw
})
another option is to put your locals into a separate file and require it then:
./locals.js
module.exports = {
foo : "some things"
}
./app.js
var express = require('express');
var app = express();
app.locals = require('./locals.js')
var route = require('./lib.route');
app.use(route);
./lib/route.js
var express = require('express');
var app = module.exports = express();
app.locals = require('./locals.js')
app.get('/route', function (req, res, next) {
assert.equal(app.locals.foo, 'some things'); // this will throw
})
because node caches the require calls every app gets the same locals hash.
there are surely more options to solve that :)
Am Sonntag, 10. März 2013 21:53:04 UTC+1 schrieb Stephen Bartell:
I was going to ask the same question.
First @Jason:
I made some middleware that, per req, will do some auth things. So I wanted the results of the auth middleware to be available for the rest of the handler stack. At the top of my handler stack, i create a `req.locals`, like app.locals and res.locals. Then middleware will hang things off of it.
For example:
app.use(function (req, res, next){
req.locals = {};
next()
})
app.use(authMiddleware);
app.use(x)
app.use(y)
Then authMiddleware will add things to req.locals for the rest of the handlers to use. I dont use any views since I make mostly web services. But res.locals is for that. So you would assign req.locals to res.locals and be on your way.
I havent seen anyone else doing this (req.locals), so to me it smells a little. But I think it makes sense since per-user things belong in the context of the request. It wouldnt really make sense to make it available globally via app.locals. If this is a bad idea could someone speak up?
Now about app.locals. My question is about app.locals when my handlers are more modular and spread accross mutliple files: ie:
./app.js
var express = require('express');
app.locals.foo = 'some things'; // i want to globalize foo.
var route = require('./lib.route');
app.use(route);
./lib/route.js
var express = require('express');
var app = module.exports = express();
app.get('/route', function (req, res, next) {
assert.equal(app.locals.foo, 'some things'); // this will throw
})
For the life of me I cant get foo inside of my other handlers (route.js)
Could someone help me see what Im doing wrong here?