Skip to content

Instantly share code, notes, and snippets.

@steckel
Created September 30, 2011 19:53
Show Gist options
  • Save steckel/1254799 to your computer and use it in GitHub Desktop.
Save steckel/1254799 to your computer and use it in GitHub Desktop.
Just a, stupid, simple executable coffeescript that'll serve up a folder on your filesystem
#!/usr/bin/env coffee
sys = require "sys"
http = require "http"
url = require "url"
path = require "path"
fs = require "fs"
argv = process.argv
init = () ->
if !argv[2] or argv[2] is '-h'
usage()
else
target = path.normalize argv[2] + '/'
runServer(target)
usage = () ->
console.log """-----------------------------------------------------------------------------------
Ohai there.
I don't know shit about fuck, when it comes to software engineering
(I'm a front end dev who likes to dabble...) but, I figured I'd share
a simple script I quickly got working that serves up static files on
your file system through node.
Usage, clearly you're running this with the coffee REPL or you've
already $chmod +x filename.coffee, so I won't go there.
Just pass the path you'd like to serve up as an argument and the thing should work.
i.e. $ ./simpleAssServer.coffee ~/Desktop/testProject
-----------------------------------------------------------------------------------
"""
runServer = (target) ->
http.createServer((request, response) ->
uri = url.parse(request.url).pathname
filename = path.join(target, uri)
path.exists filename, (exists) ->
unless exists
response.writeHead 404, "Content-Type": "text/plain"
response.write "404 Not Found\n"
response.end()
return
fs.readFile filename, "binary", (err, file) ->
if err
response.writeHead 500, "Content-Type": "text/plain"
response.write err + "\n"
response.end()
return
response.writeHead 200
response.write file, "binary"
response.end()
).listen 8080
sys.puts "Server running at http://localhost:8080/"
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment