Skip to content

Instantly share code, notes, and snippets.

@brooklynDev
Created July 27, 2012 01:12
Show Gist options
  • Save brooklynDev/3185573 to your computer and use it in GitHub Desktop.
Save brooklynDev/3185573 to your computer and use it in GitHub Desktop.
Express routes/controller initialization
//app.js
var express = require('express')
, http = require('http')
, fs = require('fs');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
});
var controllerFiles = fs.readdirSync('./controllers');
controllerFiles.forEach(function(controllerFile){
controllerFile = controllerFile.replace('.js','');
var controller = require('./controllers/' + controllerFile);
controller.setup(app);
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
//home.js in controllers directory
exports.setup = function(app){
app.get('/', function(req,res){
res.render('home');
}),
app.get('/home/about', function(req,res){
res.render('about');
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment