chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.62k stars 825 forks source link

Unable to output console.log and my plugin is not running #914

Closed proc07 closed 1 year ago

proc07 commented 1 year ago

Checks

Describe the bug (be clear and concise)

I can make a proxy request, but I can't execute my plug-in.

consle.log can output start without using the createProxyMiddleware function

截屏2023-05-10 下午4 45 18

otherwise,Used, no output

截屏2023-05-10 下午4 56 53

Step-by-step reproduction instructions

1. ...
2. ...

Expected behavior (be clear and concise)

Can execute: console.log('start') console.log(proxyRes.headers["content-encoding"]) and my plunin

How is http-proxy-middleware used in your project?

react-project@0.1.0 /Users/*******/Desktop/*******/examples/react-project
├── http-proxy-middleware@2.0.6 -> ./../../node_modules/.pnpm/http-proxy-middleware@2.0.6/node_modules/http-proxy-middleware
└─┬ react-scripts@3.4.4 -> ./../../node_modules/.pnpm/react-scripts@3.4.4_i2aqsaw42spuvzlb62z57gs7x4/node_modules/react-scripts
  ├─┬ mini-css-extract-plugin@0.9.0 -> ./../../node_modules/.pnpm/mini-css-extract-plugin@0.9.0_webpack@4.42.0/node_modules/mini-css-extract-plugin
  │ └─┬ webpack-dev-server@3.11.0 -> ./../../node_modules/.pnpm/webpack-dev-server@3.11.0_webpack@4.42.0/node_modules/webpack-dev-server
  │   └── http-proxy-middleware@0.19.1 deduped -> ./../../node_modules/.pnpm/http-proxy-middleware@0.19.1_tmpgdztspuwvsxzgjkhoqk7duq/node_modules/http-proxy-middleware
  └─┬ webpack-dev-server@3.11.0 -> ./../../node_modules/.pnpm/webpack-dev-server@3.11.0_webpack@4.42.0/node_modules/webpack-dev-server
    └── http-proxy-middleware@0.19.1 -> ./../../node_modules/.pnpm/http-proxy-middleware@0.19.1_tmpgdztspuwvsxzgjkhoqk7duq/node_modules/http-proxy-middleware

What http-proxy-middleware configuration are you using?

const path = require('path')
const {createProxyMiddleware} = require('http-proxy-middleware')
const {createRuntimeGeneratedType} = require('../../../dist/index.cjs');

const runtimeGeneratedTypePlugin = createRuntimeGeneratedType({
  dataSource: '',
  outputPath: path.resolve(__dirname, './src/types.ts'),
  baseUrl: '/api/*',
})

module.exports = function(app) {
  console.log('start')
  app.use(
    createProxyMiddleware('/api',{
      logger: console,
      logLevel: 'debug',
      target: 'http://jsonplaceholder.typicode.com',
      changeOrigin: true,
      pathRewrite: {'^/api' : ''},
      plugins: [runtimeGeneratedTypePlugin],
      on: {
        proxyRes: (proxyRes, req, res) => {
          console.log(proxyRes.headers["content-encoding"])
        },
      }
    })›
  );
};

What OS/version and node/version are you seeing the problem?

macos: 11.6.1
node: v18.15.0

Additional context (optional)

No response

chimurai commented 1 year ago

I see you have v2.0.6 installed.

Plugins are available in the beta version of v3.

You can install it with:

npm install http-proxy-middleware@beta

or

yarn add http-proxy-middleware@beta
proc07 commented 1 year ago

start [HPM] Missing "target" option. Example: {target: "http://www.example.org"}

截屏2023-05-11 下午3 56 49

http-proxy-middleware@beta i've already set it up target: 'http://jsonplaceholder.typicode.com', @chimurai

proc07 commented 1 year ago

I see you have v2.0.6 installed.

Plugins are available in the beta version of v3.

You can install it with:

npm install http-proxy-middleware@beta

or

yarn add http-proxy-middleware@beta

i used 2.0.6 and 0.20.0 version ,delete plugin code,but following code is not executed either, printing log

on: {
        proxyRes: (proxyRes, req, res) => {
          console.log(proxyRes.headers["content-encoding"])
        },
      }
   proxyRes: (proxyRes, req, res) => {
          console.log(proxyRes.headers["content-encoding"])
        },     
proc07 commented 1 year ago

solved https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md#removed-context-argument