Created
July 12, 2012 14:57
-
-
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
This file contains hidden or 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
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