Skip to content

Instantly share code, notes, and snippets.

@katanacrimson
Last active December 12, 2015 08:49
Show Gist options
  • Save katanacrimson/4746902 to your computer and use it in GitHub Desktop.
Save katanacrimson/4746902 to your computer and use it in GitHub Desktop.
var url = require('url'), path = require('path'), fs = require('fs'), port = parseInt(process.argv[2] || 8888, 10)
require('http').createServer(function(req, res) {
var rfile = path.join(__dirname, 'shareddir', url.parse(req.url).pathname)
if(!fs.existsSync(rfile)) {
res.writeHead(404, {'Content-Type': 'text/plain'})
res.write("404 Not Found\n")
res.end()
} else {
fs.readFile(rfile + (fs.statSync(rfile).isDirectory()) ? '/index.html' : '', 'binary', function(err, file) {
if(err) {
res.writeHead(500, {'Content-Type': 'text/plain'})
res.write(err + "\n")
} else {
res.writeHead(200)
res.write(file, 'binary')
}
res.end()
})
}
}).listen(port, function() { console.log("file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown") })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment