Last active
November 19, 2018 10:12
-
-
Save kn0ll/1338670 to your computer and use it in GitHub Desktop.
a simple nodejs request proxy as connect middleware. developed as a cross domain ajax proxy.
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
### | |
a small connect middleware proxy for cross domain ajax | |
@path first match group will be forwarded | |
@host the host you want to proxy | |
connect_server.use proxy '^(/.+)', 'api.twitter.com' | |
connect_server.use proxy '^/gh(/.+)', 'api.github.com' | |
### | |
https = require 'https' | |
module.exports = (path, host) -> | |
(req, res, next) -> | |
if match = req.originalUrl.match path | |
opts = | |
host: host | |
path: match[1] | |
method: req.method | |
api_req = https.request opts, (response) -> | |
body = '' | |
res.writeHead response.statusCode, | |
response.headers | |
response.on 'data', (chunk) -> | |
body += chunk | |
response.on 'end', -> | |
res.end body | |
if req.body | |
api_req.write JSON.stringify req.body | |
api_req.end() | |
else | |
next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment