Last active
December 10, 2015 13:39
-
-
Save spearwolf/4442468 to your computer and use it in GitHub Desktop.
http static file server ./public/ with node
This file contains 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 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