Skip to content
New issue

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

Proxy 404(http-proxy-middleware) #1851

Open
3 tasks done
Abbyzhoum opened this issue Dec 6, 2024 · 0 comments
Open
3 tasks done

Proxy 404(http-proxy-middleware) #1851

Abbyzhoum opened this issue Dec 6, 2024 · 0 comments
Labels

Comments

@Abbyzhoum
Copy link

Describe the bug

**Node.js version: ** 20.18.0

OS version: mccOS Sonoma 14.6

Description: use proxy, koa return 404

Actual behavior

image

use node --inspect, I know how the 404 error is returned.

image

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"
}

Expected behavior

return real proxy codeStatus and response

Code to reproduce

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)


Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@Abbyzhoum Abbyzhoum added the bug label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant