Node with Express, Jade, Stylus, and Mongoose
var express = require('express')
, path = require('path')
, stylus = require('stylus')
, nib = require('nib')
, mongoose = require('mongoose');
var app = express();
app.locals.title = 'My Web App';
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
function compile(str, path) { return stylus(str).set('filename', path).use(nib()); }
var oneDay = 86400000;
app.use( express.favicon() );
app.use( express.logger('dev') );
app.use( express.bodyParser() );
app.use( express.methodOverride() );
app.use( require('stylus').middleware({src: __dirname + '/public', compile: compile}) );
app.use( express.static(path.join(__dirname, 'public'), { maxAge: oneDay } ) );
app.use( app.router );
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// Non-static paths
app.get('/' , function(req, res) { renderPage('index' , req, res); });
app.get('/:page.html', function(req, res) { renderPage(req.params.page, req, res); });
//----------------
function renderPage(page, req, res) {
if ('development' != app.get('env')) res.setHeader('cache-control', 'public, max-age='+oneDay);
res.render(req.params.page, { page: page } );
}