chimurai / http-proxy-middleware

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

"TypeError: next is not a function" when route doesn't match filter provided to the middleware #870

Closed TomaszG closed 4 months ago

TomaszG commented 1 year ago

Checks

Describe the bug (be clear and concise)

In my project, I have configured

When I hit /api/graphql then everything works fine. But when I try to hit non-existing API endpoint, e.g. /api/something then, the http-proxy-middleware throws the following exception:

index.js?46cb:598 Uncaught TypeError: next is not a function at HttpProxyMiddleware.middleware (/Users/tomasz/Projects/http-middleware-issue/node_modules/http-proxy-middleware/dist/http-proxy-middleware.js:29:17) at Object.apiResolver (/Users/tomasz/Projects/http-middleware-issue/node_modules/next/dist/server/api-utils/node.js:372:15) at DevServer.runApi (/Users/tomasz/Projects/http-middleware-issue/node_modules/next/dist/server/next-server.js:488:27)

The error comes from this line: https://github.com/chimurai/http-proxy-middleware/blob/v2.0.6/src/http-proxy-middleware.ts#L59

This seems to be fixed some time ago in v3 but has not been released yet: https://github.com/chimurai/http-proxy-middleware/commit/4a081fe1b8622c3853c2396a21f9274e00a01df6#diff-07e6ad10bda0df091b737caed42767657cd0bd74a01246a1a0b7ab59c0f6e977R55

Is there a chance to get a 2.x release with a bug fix?

Step-by-step reproduction instructions

1. Create http-proxy-middleware with a filter
2. Fetch route that doesn't match the filter

Or follow README from https://github.com/TomaszG/http-proxy-middleware-redirect-issue

Expected behavior (be clear and concise)

No error is thrown

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

my-app@0.1.0 /Users/tomasz/Projects/http-middleware-issue
└── http-proxy-middleware@2.0.6

What http-proxy-middleware configuration are you using?

NextJS with http-proxy-middleware for backend redirects:

export default createProxyMiddleware(backendEndpoints, {
    target: apiBaseUrl,
    changeOrigin: true,
})

next.config.js is as follows:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  serverRuntimeConfig: {
    apiBaseUrl: 'http://localhost:8000',
    backendEndpoints: [
      '/api/graphql',
    ],
  },
}

module.exports = nextConfig

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

MacOS 13.1, Node 18.13.0

Additional context (optional)

No response

chimurai commented 4 months ago

Next.js is supported in: https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.0