Node.JS Image Web Server (using CoffeeScript)
Language: Plain Text
# NB: this is CoffeeScript!!
# http://jashkenas.github.com/coffee-script/
# We assume that you're storing image files in a 'test-images' directory.
sys = require 'sys'
http = require 'http'
url = require 'url'
posix = require 'posix'
workingDir: process.cwd()
getImageName: (request) ->
requestURL = url.parse request.url
pathSegs = requestURL.pathname.split '/'
pathSegs.shift()
pathSegs.shift()
http.createServer((request, response) ->
imageName: getImageName request
headers: { "Content-Type": "image/jpeg" }
imagePath: workingDir + "/test-images/" + imageName
posix.cat(imagePath, "binary").addCallback(
(data) ->
response.sendHeader 200, headers
response.sendBody data, "binary"
response.finish()
).addErrback(
(err) ->
response.sendHeader 404, headers
response.finish()
)
).listen 8000
Reveal More
