Last active
October 21, 2025 11:42
-
-
Save ravshansbox/2dba8f7c411926099bc9cf8f6f016003 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const http = require('node:http'); | |
| const https = require('node:https'); | |
| const target = 'example.com'; | |
| const corsHeaders = { 'access-control-allow-origin': '*' }; | |
| const server = http.createServer((request, response) => { | |
| if (request.method === 'OPTIONS') { | |
| response.writeHead(200, corsHeaders); | |
| response.end(); | |
| return; | |
| } | |
| const { pathname, search } = new URL( | |
| request.url, | |
| `http://${request.headers.host}`, | |
| ); | |
| const proxyRequest = https.request( | |
| { | |
| method: request.method, | |
| hostname: target, | |
| path: pathname + search, | |
| headers: { ...request.headers, host: target }, | |
| rejectUnauthorized: false, | |
| }, | |
| proxyResponse => { | |
| response.writeHead(proxyResponse.statusCode, { | |
| ...proxyResponse.headers, | |
| ...corsHeaders, | |
| }); | |
| proxyResponse.pipe(response); | |
| }, | |
| ); | |
| proxyRequest.on('error', error => { | |
| console.error('Proxy error:', error); | |
| response.writeHead(500); | |
| response.end('Proxy error'); | |
| }); | |
| request.pipe(proxyRequest); | |
| }); | |
| server.listen(3001, '0.0.0.0', () => { | |
| console.log(`Proxy server running on`, server.address()); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment