I'm writing a mudule express-based NodeJS app. Here are important parts of my app.js:
var express = require('express')
, routes = require('./routes')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
var app = module.exports = express.createServer();
var ab = 'this is a test!';
// Configuration
app.configure(function(){
...
// Routes
app.get('/', routes.index);
app.listen(3000);
Routes.index - is a controller, that executes when '/' is requested. Here is the code:
exports.index = function(req, res){
passport.serializeUser(function(user, done) {
done(null, user.id);
});
...
res.render('index.ejs', {
title: ab
})
};
Techically, index.js - is separate file, located in '/routes' folder. So, when I launch my app, it crashed cause can't find passport var, declared in main app. Also, ab also can't be found, however it was declared. If I re-declate vars in index.js, JS will create new objects for me. How can I use my vars in every module of my app? I looked through a few topics on SO, however couldn't understand – is it a common problem or just a structure of my app is wrong? Thanks!