You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My way of using the proxy is not returning post result from upstream server. The server has processed the request and has returned something that the proxy never pushed back the client. Can someone review the code below:
function startProxy() {
const app = express()
// Set view engine
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
// Middleware to Proxy Requests, including WebSockets
app.use(async (req, res, next) => {
const urlPath = req.path.split('/')[1];
if (!urlPath) return next();
const server = serverManager.getRunningServers().find(s => s.name === urlPath);
if (server) {
const target = `http://localhost:${server.port}`;
console.log(`[proxy] for "${server.name}" targeting ${target}`);
if (!proxyCache.has(server.name)) {
console.log(`Creating proxy middleware for "${server.name}" targeting ${target}`);
const proxy = createProxyMiddleware({
target,
changeOrigin: true,
ws: true,
pathRewrite: {
// [`^/${server.name}`]: '', // Uncomment if needed
},
timeout: 5000, // Time to wait before timing out
proxyTimeout: 5000, // Timeout for the target server response
selfHandleResponse: false, // Proxy should handle response piping
});
proxyCache.set(server.name, proxy);
}
return proxyCache.get(server.name)(req, res, next);
}
else {
next();
}
});
// Middleware to parse JSON bodies
// app.use(bodyParser.json())
// Dashboard Route
app.get('/', (req, res) => {
const servers = serverManager.getRunningServers()
res.render('dashboard', { servers })
})
// Stop Server Route
app.post('/stop/:name', async (req, res) => {
const { name } = req.params
serverManager.stopServer(name)
res.sendStatus(200)
})
// Restart Server Route (Optional)
app.post('/restart/:name', async (req, res) => {
const { name } = req.params
await serverManager.restartServer(name)
res.sendStatus(200)
})
// Start Proxy Server
const server = app.listen(PROXY_PORT, () => {
console.log(`Proxy server is running at http://localhost:${PROXY_PORT}`)
})
server.on('upgrade', (req, socket, head) => {
const urlPath = req.url?.split('/')[1];
console.debug('in upgrade on url:', req.url, urlPath);
if (!urlPath) {
socket.destroy();
return;
}
const serverInstance = serverManager.getRunningServers().find(s => s.name === urlPath);
if (serverInstance) {
const target = `http://localhost:${serverInstance.port}`;
console.log(`Proxying WebSocket for "${serverInstance.name}" to ${target}`);
// Use cached proxy if available, otherwise create and cache it
const proxy = createProxyMiddleware({
target,
changeOrigin: true,
ws: true,
pathRewrite: {
[`^/${serverInstance.name}`]: '',
},
});
// Use the cached proxy for the WebSocket upgrade
proxy.upgrade(req, socket as any, head);
} else {
console.debug(`No server instance found for path: ${urlPath}`);
socket.destroy();
}
});
return server
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
My way of using the proxy is not returning post result from upstream server. The server has processed the request and has returned something that the proxy never pushed back the client. Can someone review the code below:
Beta Was this translation helpful? Give feedback.
All reactions