Skip to content

Instantly share code, notes, and snippets.

@kixxauth
Created June 25, 2011 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kixxauth/1046996 to your computer and use it in GitHub Desktop.
Save kixxauth/1046996 to your computer and use it in GitHub Desktop.
Hanging responses from Node-static
# This is a factory function which creates a middleware handler
# for serving static files.
#
# We have to use staticServer.serveFile() instead of .serve() because
# we are dynamically rewriting the file path based on the name of the
# application asking for the file.
resourceHandler = (opts) ->
staticServer = new static.Server(STATIC_PATH)
handler = (req, res, next) ->
assetPath = utils.createAssetPath(req.url, req.params.appname)
resolved = false
req.on 'end', ->
fileHandle = staticServer.serveFile(assetPath, 200, {}, req, res)
fileHandle.on 'error', (err) ->
if resolved then return
resolved = true
if err.code is 'ENOENT'
return next(new Error('Not Found'))
logging.error("Static server error")
logging.debug(err)
return next(err)
fileHandle.on 'success', (result) ->
resolved = true
return
return
return
return handler
# This is the proxy request function. It is a method of a class which
# extends the EventEmitter for better utility.
proxyRequest: (request, response, options) ->
proxy = @
host = options.host
port = options.port
proxyOpts =
host: host
port: port
method: request.method
path: request.url
headers: request.headers
reverseProxy = http.request proxyOpts, (proxyResponse) ->
response.writeHead(proxyResponse.statusCode, proxyResponse.headers)
proxyResponse.pipe(response)
return
reverseProxy.once 'error', (err) ->
response.writeHead(500)
response.end()
proxy.emit('error', err)
return
request.pipe(reverseProxy)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment