Skip to content

Instantly share code, notes, and snippets.

@mediaupstream
Last active February 26, 2018 22:16
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 mediaupstream/18c57b85e41df531482305cc4d26eecf to your computer and use it in GitHub Desktop.
Save mediaupstream/18c57b85e41df531482305cc4d26eecf to your computer and use it in GitHub Desktop.
const express = require('express')
const request = require('request')
// APP 1 (the proxy server)
const app1 = express()
app.use('/proxy', (req, res) => {
let target = req.url.replace('/', 'http://')
req.pipe(request(target)).pipe(res)
})
app1.listen(3000, () => console.log('app1: localhost:3000'))
// APP 2 (the target server)
const app2 = express()
app2.all('/*', (req, res) => {
console.log('received thing', req.url)
res.send('Server Online')
})
app2.listen(3020, () => console.log('app2: localhost:3020'))

The script creates two servers, one on localhost:3000 and another on port 3020.

You can use app1 to proxy a request to app2 (or any other server):

curl localhost:3000/proxy/localhost:3020/health

The app2 is just for example purposes. You should be able to proxy requests to anywhere:

curl http://localhost:3000/proxy/jsonplaceholder.typicode.com/posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment