dev.pus
dev.pus

Reputation: 8139

How to adminstrate configuration in node/express

I would like to remove the configuration out of my app.js and split it into severall files:

/config/app.js (for the whole app.configure(function(){});)

and /config/view.js (for the template engine configuration)

Unfortunatly javascript doesn't know any include function and I would like to avoid to write an own.

So what is the best approach do split the configuration from app.js?

config.js

module.exports = function(app, express, passport){
app.configure(function(){
    app.use(express.logger());
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({ secret: 'kitty'}));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
    app.use(express.errorHandler());
    app.set('views', __dirname + '/views');
    app.set('view options', { layout: false });
});
};

var config = require('./config/app.js')
var app = module.exports = express.createServer();

app.js

var app = express.createServer();
var config = require('./config/app.js')(app, express, passport);

Upvotes: 1

Views: 326

Answers (1)

freakish
freakish

Reputation: 56477

You seem to use require incorrectly. Do it like this:

app.js

var app = express.createServer();
var config = require('./config')(app);

config.js

module.exports = function(app) {
    app.configure(function () {
        app.use(express.logger());
        /* etc */
    });
};

Upvotes: 2

Related Questions