Last active
March 10, 2022 06:29
-
-
Save randName/88c7c2fc7a30890ed31b0dae14b29d24 to your computer and use it in GitHub Desktop.
cors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "@musakui/cors", | |
"version": "0.0.0", | |
"type": "module", | |
"bin": "./server.js" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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