Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created July 26, 2012 13:14
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 basicxman/3181990 to your computer and use it in GitHub Desktop.
Save basicxman/3181990 to your computer and use it in GitHub Desktop.
http = require("http")
fs = require("fs")
port = undefined
readPort = ->
fs.readFile "port", (err, data) ->
port = Number(data)
readPort()
fs.watch "port", (event, filename) ->
readPort()
s = http.createServer (req, res) ->
console.log "Request received, #{req.method} #{req.url}"
opts = {
port: port
, method: req.method
, path: req.url
, headers: req.headers
}
proxy_req = http.request opts, (proxy_res) ->
proxy_res.on "data", (chunk) ->
for key, value of proxy_res.headers
res.setHeader(key, value)
res.write(chunk, "binary")
res.end()
proxy_req.on "error", (e) ->
res.writeHead(500)
res.end()
req.on "data", (chunk) ->
proxy_req.write(chunk, "binary")
req.on "end", ->
proxy_req.end()
s.listen(3000, '127.0.0.1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment