Skip to content

Instantly share code, notes, and snippets.

@spearwolf
Last active December 10, 2015 13:39
Show Gist options
  • Save spearwolf/4442468 to your computer and use it in GitHub Desktop.
Save spearwolf/4442468 to your computer and use it in GitHub Desktop.
http static file server ./public/ with node
var http = require('http'),
send = require('send'), // npm install send
url = require('url'),
app = http.createServer(function(req, res){
function redirect() {
res.statusCode = 301;
res.setHeader('Location', req.url + '/');
res.end('Redirecting to ' + req.url + '/');
}
send(req, url.parse(req.url).pathname)
.root('./public')
.on('directory', redirect)
.pipe(res);
});
app.listen(1975, "0.0.0.0");
console.log("try http://localhost:1975/");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment