Spaces:
Running
Running
const fastify = require('fastify')({ logger: true }); | |
const http = require('http'); | |
const { spawn } = require('child_process'); | |
const PROXY_PORT = 7860; // Change this to a port different from 1688 | |
const TARGET_HOST = '127.0.0.1'; | |
const TARGET_PORT = 1688; | |
// Function to start the request forwarding server | |
async function startRequestForwardingServer() { | |
const server = http.createServer((req, res) => { | |
const options = { | |
hostname: TARGET_HOST, | |
port: TARGET_PORT, | |
path: req.url, | |
method: req.method, | |
headers: req.headers, | |
}; | |
const proxy = http.request(options, (proxyRes) => { | |
res.writeHead(proxyRes.statusCode, proxyRes.headers); | |
proxyRes.pipe(res, { end: true }); | |
}); | |
req.pipe(proxy, { end: true }); | |
}); | |
server.listen(PROXY_PORT, '0.0.0.0'); | |
console.log(`Request forwarding server listening on port ${PROXY_PORT}`); | |
} | |
// Function to start the background process | |
function startYarnStartProcess() { | |
const child = spawn('yarn', ['start'], { | |
detached: true, | |
stdio: 'ignore' | |
}); | |
child.unref(); | |
} | |
// Main function to start everything | |
async function startServers() { | |
try { | |
startYarnStartProcess(); | |
await startRequestForwardingServer(); | |
} catch (error) { | |
console.error('Failed to start servers:', error); | |
} | |
} | |
// Start the servers | |
startServers(); | |