Skip to content

Releases: stegano/next-http-proxy-middleware

v1.0.4

09 May 08:18
Compare
Choose a tag to compare

v1.0.4

Fixes

  • Resolves an issue with no response from the target server when a put or post method is requested (#3)

v1.0.0

25 Feb 03:42
Compare
Choose a tag to compare

Next.js HTTP Proxy Middleware

HTTP Proxy middleware available in API Middleware provided by Next.js.

Installation

The easiest way to install next-http-proxy-middleware is with npm.

npm install next-http-proxy-middleware

Alternately, download the source.

git clone https://github.com/stegano/next-http-proxy-middleware.git

Features

This middleware is implemented using the http-proxy library. You can use the existing options provided by http-proxy. And you can rewrite the api path using pathRewrite, an additional option provided by this middleware.

Example

// pages/[...all].ts
...

export default (req: NextApiRequest, res: NextApiResponse) => (
  isDevelopment
    ? httpProxyMiddleware(req, res, {
      target: 'https://www.example.com',
      pathRewrite: {
        '^/api/new': '/v2', // `/api/new/test` -> `/v2/test`
        '^/api': '', // `/api/test` -> `/test`
      },
    })
    : res.status(404).send(null)
);