Skip to content

Instantly share code, notes, and snippets.

@randName
Last active March 10, 2022 06:29
Show Gist options
  • Save randName/88c7c2fc7a30890ed31b0dae14b29d24 to your computer and use it in GitHub Desktop.
Save randName/88c7c2fc7a30890ed31b0dae14b29d24 to your computer and use it in GitHub Desktop.
cors
{
"name": "@musakui/cors",
"version": "0.0.0",
"type": "module",
"bin": "./server.js"
}
#!/usr/bin/env node
import { createServer } from 'http'
import { request } from 'https'
const port = parseInt(process.argv[2]) || 8000
createServer((req, res) => {
const { url, ...opts } = Object.fromEntries(new URLSearchParams(req.url.slice(1)))
if (!url) return res.writeHead(204).end()
try {
request(url, opts, (r) => r.pipe(res.writeHead(r.statusCode, r.headers))).end()
} catch (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' }).end(`${err}`)
}
}).listen(port, '0.0.0.0', () => console.log(`Server running on port ${port} ...`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment