Skip to content

Instantly share code, notes, and snippets.

@hasantayyar
Forked from kn0ll/proxy.coffee
Created November 19, 2018 10:12
Show Gist options
  • Save hasantayyar/fc3ca1ad1190f57df689645507f3286d to your computer and use it in GitHub Desktop.
Save hasantayyar/fc3ca1ad1190f57df689645507f3286d to your computer and use it in GitHub Desktop.

Revisions

  1. nic luciano renamed this gist Jan 30, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. nic luciano revised this gist Nov 4, 2011. 1 changed file with 3 additions and 8 deletions.
    11 changes: 3 additions & 8 deletions tinyproxy.coffee
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,11 @@
    ###
    a small connect middleware proxy for cross domain ajax
    examples:
    connect_server.use proxy '^(/.+)', 'api.twitter.com'
    connect_server.use proxy '^/gh(/.+)', 'api.github.com'
    @path regex of the path your proxy should address
    the first match group will be the forwarded url
    @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'
  3. nic luciano created this gist Nov 4, 2011.
    40 changes: 40 additions & 0 deletions tinyproxy.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    ###
    a small connect middleware proxy for cross domain ajax
    examples:
    connect_server.use proxy '^(/.+)', 'api.twitter.com'
    connect_server.use proxy '^/gh(/.+)', 'api.github.com'
    @path regex of the path your proxy should address
    the first match group will be the forwarded url
    @host the host you want to proxy
    ###

    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()