Created
July 27, 2012 01:12
-
-
Save brooklynDev/3185573 to your computer and use it in GitHub Desktop.
Express routes/controller initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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