vagusX / koa-proxies

a koa@2.x+ proxy middleware
https://vagusx.github.io/koa-proxies/
MIT License
161 stars 43 forks source link

ctx refrence error #73

Closed imshenshen closed 5 months ago

imshenshen commented 1 year ago

const querystring = require('querystring')
const proxy = require('koa-proxies')

module.exports = (app) => {
  ......
  app.router.all(
    `${config.prefix}/(.*)`,
    proxy(`${config.prefix}/(.*)`, (params, ctx) => {

     console.log('return options function call : ' + ctx.url)

      return {
        logs: true,
        changeOrigin: true,
        target: config.uasSvc,
        events: {
          proxyReq(proxyReq, req, res) {

            console.log('proxy req event call: ' + ctx.url)

          },
        },
      }
    })
  )
}

I fetch twice with different url, why the secondary proxyReq function output is the FIRST fetch's url ? CleanShot 2023-07-20 at 16 18 16@2x

output:

CleanShot 2023-07-20 at 16 19 02@2x

fyl080801 commented 1 year ago

https://github.com/vagusX/koa-proxies/blob/15d3f1c8dc672150432fc6924c752b57230ad46a/index.js#L86-L96

in line 94, the first invocation of the event is cached here. If the method uses the 'ctx' passed from the previous options, the 'ctx' object inside it remains the same as the first time every time it is executed.