Skip to content

Instantly share code, notes, and snippets.

@mauriciomassaia
Last active October 5, 2021 07:38
Show Gist options
  • Save mauriciomassaia/8e36ad6c3e7f671a1d31b562a9b8bf96 to your computer and use it in GitHub Desktop.
Save mauriciomassaia/8e36ad6c3e7f671a1d31b562a9b8bf96 to your computer and use it in GitHub Desktop.
Local node proxy server for development
const http = require('http')
const https = require('https')
const PROXY_PORT = 8080
const onRequest = (clientReq, clientRes) => {
console.log(`onRequest: ${clientReq.url}`)
clientRes.setHeader('Access-Control-Allow-Origin', '*')
clientRes.setHeader('Access-Control-Request-Method', '*')
clientRes.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
clientRes.setHeader('Access-Control-Allow-Headers', '*')
const options = {
hostname: 'yourdomain.com',
port: 443,
path: clientReq.url,
method: 'GET'
}
const proxy = https.request(options, (res) => {
clientRes.writeHead(res.statusCode, res.headers)
res.pipe(clientRes, { end: true })
})
clientReq.pipe(proxy, { end: true })
}
http.createServer(onRequest).listen(PROXY_PORT)
console.log(`Proxy Server created on http://localhost:${PROXY_PORT}!`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment