var express = require('express') , routes = require('./routes') , http = require('http') , url = require('url'); 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.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(urlgenerator); app.use(app.router); app.use(express.static(__dirname + '/public', {maxAge: 60 * 60 * 1000})); }); app.configure('development', function(){ app.use(express.errorHandler()); }); function urlgenerator(req, res, next) { var host = req.headers.host; res.urlgenerator = { start: function() { return 'http://' + host + '/'; }, departments: function(query) { return url.format({ protocol: 'http', host: host, pathname: '/department/', query: query }); }, department: function(dept_no) { return 'http://' + host + '/department/' + dept_no; }, employees_in_department: function(dept_no, query) { return url.format({ protocol: 'http', host: host, pathname: '/department/' + dept_no + '/employees', query: query }); }, employees: function(query) { return url.format({ protocol: 'http', host: host, pathname: '/employee/', query: query }); }, employee: function(emp_no) { return 'http://' + host + '/employee/' + emp_no; } }; next(); } app.get('/', routes.index); app.get('/department', routes.departments); app.get('/department/:dept_no', routes.department); app.get('/department/:dept_no/employees', routes.employees_in_department); app.get('/employee', routes.employees); app.get('/employee/:emp_no', routes.employee); http.createServer(app).listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });