Created
November 16, 2012 10:11
-
-
Save alexaivars/4086162 to your computer and use it in GitHub Desktop.
a simple node.js ajax get to post proxy server.
This file contains hidden or 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") | |
q = require("querystring") | |
url = require("url") | |
server = http.createServer().listen 9006 | |
server.on "request", (req, res) -> | |
params = url.parse(req.url, true).query | |
callbackFn = "callback" | |
if params.url is undefined or params.url is "" | |
res.write "missing target url" | |
res.end() | |
return | |
if params.data is undefined or params.data is "" | |
res.write "missing post data" | |
res.end() | |
return | |
if params.callback isnt undefined and params.callback isnt "" | |
callbackFn = params.callback | |
# console.log callbackFn | |
requestUrl = url.parse(params.url) | |
# console.log requestUrl | |
post_data = params.data | |
options = | |
host: requestUrl.hostname | |
path: requestUrl.path | |
method: "POST" | |
headers: | |
"Content-Length": post_data.length | |
"Content-Type": "application/json" | |
client = http.request(options) | |
client.on "response", (response) -> | |
data = "" | |
response.setEncoding('utf8') | |
response.on "data", (chunk) -> | |
data += chunk | |
response.on "end", () => | |
@emit("end",data) | |
client.write(post_data) | |
client.end() | |
client.on "end", (data) -> | |
res.write "#{callbackFn} (#{data})" | |
res.end() | |
console.log "post proxy running on 9006" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment