Skip to content

Instantly share code, notes, and snippets.

@dk00
Created January 11, 2019 02:40
Show Gist options
  • Save dk00/38982661cf8575002196d250581eb070 to your computer and use it in GitHub Desktop.
Save dk00/38982661cf8575002196d250581eb070 to your computer and use it in GitHub Desktop.
Free CORS proxy for everyone
*.log
.pnp*
node_modules
coverage
.nyc_output
dist
lib
bin
*.tgz
tmp
.DS_Store
thumbs.db
{
"name": "xors",
"version": "0.0.1",
"main": "server.js",
"license": "Unlicense",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.16.4",
"node-fetch": "^2.3.0"
}
}
const express = require('express')
const cors = require('cors')
const fetch = require('node-fetch')
const toObject = it =>
Object.assign(...Array.from(
it.entries(),
([key, value]) => ({[key]: value})
))
const app = express()
app.use(cors({origin: true}))
app.use(/\/http.+/, (req, res, next) => {
const url = decodeURIComponent(req.originalUrl.slice(1))
const init = {
method: req.method,
headers: req.headers,
...(/head|get/i.test(req.method)? {}: {body: req})
}
fetch(url, init)
.then(result => {
res.status(result.status)
res.set(toObject(result.headers))
result.body.pipe(res)
})
.catch(e => {
console.log(e)
res.status(502).json(e)
})
})
app.listen(process.env.PORT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment