Skip to content

Instantly share code, notes, and snippets.

@pyykkis
Created September 2, 2012 15:54
Show Gist options
  • Save pyykkis/3600830 to your computer and use it in GitHub Desktop.
Save pyykkis/3600830 to your computer and use it in GitHub Desktop.
Node.js httpServer middleware as it should be
curl localhost:3000
DEBUG: Starting child process with 'coffee server.coffee'
BAR 127.0.0.1 -- [Sun Sep 02 2012 20:31:09 GMT+0300 (EEST)] GET / HTTP/1.1
FOO 127.0.0.1 -- [Sun Sep 02 2012 20:31:09 GMT+0300 (EEST)] GET / HTTP/1.1
http = require 'http'
useRequestLogger = (handler, name) ->
(req, res) ->
console.log name, req.connection.remoteAddress, "--", "[#{new Date}]", req.method, req.url, "HTTP/#{req.httpVersion}"
handler req, res
app = (req, res) ->
setTimeout (-> res.end "World!\n"), 1000
setTimeout (-> res.write "Hello, "), 500
app = useRequestLogger app, "FOO"
app = useRequestLogger app, "BAR"
server = http.createServer app
server.listen 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment