var accept = require('http-accept') , 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()); // Unfuck the stuff that express injects app.use(function (req, res, next) { res.removeHeader("X-Powered-By"); next(); }); app.use(urlgenerator); app.use(accept); 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; }, employeesInDepartment: 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.all('/', routes.index); app.all('/department', routes.departments); app.all('/department/:dept_no', routes.department); app.all('/department/:dept_no/employees', routes.employeesInDepartment); app.all('/employee', routes.employees); app.all('/employee/:emp_no', routes.employee); http.createServer(app).listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });