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
I don't know why but we we use route as path like /forestfire/socket.io it will throw invalid header in some of my socket.io microservice but not other why i am not sure?
server.ts
import express, { Application, NextFunction, Request, Response } from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
import cors from 'cors'
import cookieParser from 'cookie-parser'
import setHeaders from 'libs/set-headers'
import { PORT } from 'config/config'
import { ClientRequest, IncomingMessage, Server } from 'http'
import routes from 'config/routes'
import accessConfig, { verifyToken } from 'libs/access-config'
import EventEmitter from 'events'
// default is 12 is not enough for this project cause we use a lot of proxy
EventEmitter.defaultMaxListeners = 20
const proxyReq = (openAccess: boolean | Array<string> | undefined) => (proxyReq: ClientRequest, req: Request, res: Response) => {
// accessConfig(openAccess)(req, res) // check if user has access to the route
if(openAccess === true) return;
if(Array.isArray(openAccess)){
if(openAccess.includes(req.path)) return;
}
// check auth token;
const token = req.cookies.alert_token
if(token){
const data = verifyToken(token)
if(data) proxyReq.setHeader('data', JSON.stringify({ user: data}))
}
}
const proxyRes = (proxyRes: IncomingMessage, req: Request, res: Response) => {
// for(let oldKey in proxyRes.headers){
// if(oldKey === 'data') continue;
// let newkey = oldKey.replace(/((?:^|-)[a-z])/g, function(val) { return val.toUpperCase() });
// newkey = newkey.replace(/(-Os-)/g, function (val) { return val.toUpperCase() });
// proxyRes.headers[newkey] = proxyRes.headers[oldKey];
// delete proxyRes.headers[oldKey];
// }
}
const start = (): Promise<Server> => {
return new Promise((resolve, reject) => {
const app: Application = express();
app.use(cors({
origin: ['http://localhost:3000', 'http://localhost:3001', 'http://192.168.4.161:3000'],
credentials: true,
}))
app.use(cookieParser())
app.use(setHeaders)
routes.forEach(({ route, target, pathRewrite, openAccess }) => {
let rewrite_path: Record<string, string> | undefined = undefined
// HPM (HTTP PROXY MIDDLEWARE)
if(!target) return console.log(`[HPM] Proxy target not found for route: ${route} -> ${target}`)
// add logs for each proxy
console.log(`[HPM] Proxy created: ${route} -> ${target}`)
if(pathRewrite) {
const [key, value] = pathRewrite
rewrite_path = { [key]: value }
// console.log(`[HPM] Proxy rewrite rule created: "${key}" ~> "${value}"`)
}
// if(route === '/'){
// // use this / routes for the main frontend app if we not use like this it will thorow Invalid frame header
// app.use(route, createProxyMiddleware({ target, changeOrigin: true, logger: console, ws: false, on: { proxyReq: proxyReq(openAccess), proxyRes } }))
// return;
// }
const proxyMiddleware = createProxyMiddleware({
target: target,
pathFilter: route,
changeOrigin: true,
pathRewrite: rewrite_path,
// timeout: 1000 * 60, // 1 minute
// proxyTimeout: 1000 * 60 * 2, // default 2 minute
ws: route === '/' ? true : true, // for websocket proxy
logger: console,
on: {
proxyReq: proxyReq(openAccess),
proxyRes
}
})
app.use(proxyMiddleware)
})
const server = app.listen(PORT, () => resolve(server))
})
}
export { start }
when i disable websocket in / path it, the invalid headers error resolved but i like to tell you this why its happening i don't know please review once
or tell me the if i am doing somthing wrong
its happen to /auth/socket.io only i don't know why but in /auth microservice credentials is true.
but when i directly connect that invalid header error is not coming
Step-by-step reproduction instructions
1. use the following code which is provided in decription.
2. ...
Expected behavior (be clear and concise)
http proxy is worked fine & other socket.io microservice is working also good but, which /auth/socket.io it will throw warning when you enable ws in / routes
How is http-proxy-middleware used in your project?
i have personal project which have more then 12 microservices so i used for centralized the services.
What http-proxy-middleware configuration are you using?
Checks
http-proxy-middleware
.Describe the bug (be clear and concise)
I don't know why but we we use route as path like /forestfire/socket.io it will throw invalid header in some of my socket.io microservice but not other why i am not sure?
server.ts
routes.ts
this is my code
when i disable websocket in / path it, the invalid headers error resolved but i like to tell you this why its happening i don't know please review once
or tell me the if i am doing somthing wrong
its happen to /auth/socket.io only i don't know why but in /auth microservice credentials is true.
but when i directly connect that invalid header error is not coming
Step-by-step reproduction instructions
1. use the following code which is provided in decription. 2. ...
Expected behavior (be clear and concise)
http proxy is worked fine & other socket.io microservice is working also good but, which /auth/socket.io it will throw warning when you enable ws in / routes
How is http-proxy-middleware used in your project?
What http-proxy-middleware configuration are you using?
What OS/version and node/version are you seeing the problem?
Ubuntu 24.04.2 LTS x86_64 & node 23
Additional context (optional)
No response
The text was updated successfully, but these errors were encountered: