We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
**Node.js version: ** 20.18.0
OS version: mccOS Sonoma 14.6
Description: use proxy, koa return 404
use node --inspect, I know how the 404 error is returned.
node --inspect
Test the proxy service api is available.
fetch("http://11.91.254.50:8091/p/security/v2/login", { "headers": { "___internal-request-id": "80a3bca2-8d01-4915-aaee-f275a218a303", "accept": "*/*", "accept-language": "zh-CN,zh;q=0.9,en;q=0.8", "cache-control": "no-cache", "content-type": "application/json", "pragma": "no-cache" }, "referrerPolicy": "no-referrer", "body": "{\"agentNo\":\"51230\",\"password\":\"Aa123456\",\"namespace\":\"lf.com\"}", "method": "POST", "mode": "cors", "credentials": "omit" }); ------------------response----------------------- { "errorMessage":"GET方法不支持,支持的方法有:POST", "errorCode":405, "path":"/security/v2/security/login", "instanceId":"security-6cdbddb8fc-9nfv4", "exceptionName":"methodNotSupportedException" }
return real proxy codeStatus and response
import { createProxyMiddleware } from 'http-proxy-middleware' import Koa from 'koa' import json from 'koa-json' import bodyparser from 'koa-bodyparser' import { getEnv } from './env' const app = new Koa() app.use(bodyparser()) app.use(json()) const env = getEnv() const proxyMiddleware = createProxyMiddleware({ target: env.nginxAddress, // proxy changeOrigin: true, logger: console, on:{ 'error':(err)=>{ console.log(err) } } }); app.use((ctx, next) => { ctx.set('Access-Control-Allow-Origin', '*') ctx.set('Access-Control-Allow-Headers', 'Content-Type, ssid') ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS') // console.log(`接口:${ctx.request.url},方法:${ctx.request.method}`); if (ctx.method == 'OPTIONS') { ctx.body = 204 } else { next() } }).use((ctx, next) => { let path = ctx.request.url; if (path.startsWith('/p/security/v2/login')) { const body = ctx.request.body as { agentNo:string } const contain = env.agentRoster.split(',').includes(body.agentNo) if(env.agentRoster && contain){ return next() } console.log(`${ctx.request.url} 走真实路径`) return proxyMiddleware(ctx.req, ctx.res, next); } next() }) setupGlobalRouter(app) const server = app.listen(8080, () => console.log('Server is running on http://localhost:8080')) server.on('upgrade', upgrade)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Describe the bug
**Node.js version: ** 20.18.0
OS version: mccOS Sonoma 14.6
Description: use proxy, koa return 404
Actual behavior
use
node --inspect
, I know how the 404 error is returned.Test the proxy service api is available.
Expected behavior
return real proxy codeStatus and response
Code to reproduce
Checklist
The text was updated successfully, but these errors were encountered: