koa-proxies icon indicating copy to clipboard operation
koa-proxies copied to clipboard

ctx refrence error

Open imshenshen opened this issue 11 months ago • 1 comments


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

imshenshen avatar Jul 20 '23 08:07 imshenshen

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.

fyl080801 avatar Jul 20 '23 09:07 fyl080801