Created
July 26, 2012 13:14
-
-
Save basicxman/3181990 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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