Skip to content

Instantly share code, notes, and snippets.

@hoangddt
Forked from mpj/README.md
Created April 26, 2016 17:44
Show Gist options
  • Save hoangddt/f5d51d5e40f2cdb8ee29e4ad50b44e78 to your computer and use it in GitHub Desktop.
Save hoangddt/f5d51d5e40f2cdb8ee29e4ad50b44e78 to your computer and use it in GitHub Desktop.
REALLY quick and dirty static file dev server

Usage

node dirtyserve.js

The point your browser to

http://localhost:3000/myfile.xxx

and the server will try to serve that file from the local server.

var http = require('http')
var fs = require('fs')
http.createServer(function (req, res) {
var filename = req.url.substring(1)
if (!fs.existsSync(filename)) {
res.writeHead(404)
res.end(filename+' not found')
return
}
var contents = fs.readFileSync(filename)
var fileEnding = req.url.match(/\.(.+)$/)[1]
res.writeHead(200, { 'Content-Type': 'text/'+ fileEnding })
res.end(contents)
}).listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment