Setting template variables from routes/index in express 4

I'm new to node.js and express. I've installed express 4 using the "express myAppName" command from the terminal, which creates a default directory, and I'm using Jade templates by default.

The main file, app.js, looks like this (standard express boilerplate):

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

// Here is my local variable assignment:
app.locals.myVariable = "Hello this is a variable";

module.exports = app;

I call my jade template from /routes/index.js:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

/* GET home page. */
router.get('/help', function(req, res) {
  res.render('index', { title: 'Help me' });
});

module.exports = router;

And my jade template has no problem rendering the value in myVariable:

extends layout

block content
  h1= title
  p Welcome to #{title}
  p #{myVariable}

However, I'd like to be able to set local variables from routes/index.js as well. I tried doing this:

var app = require('../app'); 
app.locals.myVariable = "This is my variable";

This should be importing the app.js because of module.exports = app; right? However, I get TypeError: Cannot set property 'myVariable' of undefined.

My other attempts:

router.locals.myVariable = 'This is my variable';

Seems like router does a lot of things app does, but this yields the same error.

router.set('myVariable', 'This is my variable');

I get: TypeError: Object function router(req, res, next) { router.handle(req, res, next); } has no method 'set'

What is the best way for me to set app.locals (or some equivalent) from routes/index.js?

You can try and pass your app into an init() our setup(). Also, wrap all route declarations into that setup.

I.e. routes/index.js:

// top
var app;

// later
module.exports.setup = function (appParam) {
     app = appParam;

    router.get('/', function(req, res) {

        app.locals.foo = 'bar';
        res.render('index', {baz: 'puf'});
    });
};

Call routes.setup(app) to setup that route.