Skip to content

Instantly share code, notes, and snippets.

@lhitchon
Created July 12, 2012 14:57
Show Gist options
  • Save lhitchon/3098643 to your computer and use it in GitHub Desktop.
Save lhitchon/3098643 to your computer and use it in GitHub Desktop.
docs server that automatically redirects to url with .html if that file exists
var connect = require('connect'),
fs = require('fs');
var serve_html = function(root) {
return function(req,res,next) {
var path = root + req.url + '.html';
fs.stat(path,function(err,stat) {
if (!err && !stat.isDirectory()) {
res.statusCode = 301;
res.setHeader('Location', req.url + '.html');
res.end('Redirecting to ' + req.url + '.html');
return;
}
next();
});
}
};
var port = process.env.VCAP_APP_PORT || 3000;
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('_site'))
.use(serve_html('_site'))
.use(function(req, res){
res.statusCode = 404;
res.end('Not found\n');
})
.listen(port);
console.log("Listening on %s",port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment