nhp
next-http-proxy-middleware
npm i next-http-proxy-middleware
nhp

next-http-proxy-middleware

Nextjs HTTP Proxy Middleware

by stegano

1.2.4 (see all)License:MITTypeScript:Built-InCategories:Next.js HTTP Proxy
npm i next-http-proxy-middleware
Readme

Next.js HTTP Proxy Middleware

All Contributors

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.

pathRewrite option

  • Replaces URL information matching the pattern with another string.
    • Priority is determined in the order entered in the array.
    • If the request URL matches the pattern pathRewrite.patternStr replace the URL string with the value pathRewrite.replaceStr.

onProxyInit option

  • You can access the http-proxy instance using the onProxyInit option. See the example below.

    const handleProxyInit = (proxy: httpProxy) => {
      /**
       * Check the list of bindable events in the `http-proxy` specification.
       * @see https://www.npmjs.com/package/http-proxy#listening-for-proxy-events
       */
      proxy.on('proxyReq', (proxyReq, req, res) => {
        ...
      });
      proxy.on('proxyRes', (proxyRes, req, res) => {
        ...
      });
    };
    
    export default async (req: NextApiRequest, res: NextApiResponse) 
      => httpProxyMiddleware(req, res, {
        ...
        target: 'http://example.com',
        onProxyInit: handleProxyInit,
      }
    );
    

Example

  • Refer to the following for how to use Next.js API Middleware

    // pages/api/[...all].ts
    export const config = {
      api: {
        // Enable `externalResolver` option in Next.js
        externalResolver: true,
      },
    }
    
    export default (req: NextApiRequest, res: NextApiResponse) => (
      isDevelopment
        ? httpProxyMiddleware(req, res, {
          // You can use the `http-proxy` option
          target: 'https://www.example.com',
          // In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware`
          pathRewrite: [{
            patternStr: '^/api/new',
            replaceStr: '/v2'
          }, {
            patternStr: '^/api',
            replaceStr: ''
          }],
        })
        : res.status(404).send(null)
    );
    

Using multipart/form-data

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Denny Lim

🐛 💻

Kristian Tryggestad

🐛 💻

Gunnlaugur Thor Briem

💻 🤔

Otto von Wesendonk

🛡️

Daniel Silva

🤔

Yann Pringault

💻

Lorenzo

📖

Timon Grassl

🐛

Abhinav Kumar

📖

Jack Cuthbert

📖

Vytenis

📖

Dario Varotto

📖

johannbrynjar

🐛 💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Downloads/wk

14.9K

GitHub Stars

0
No data available

LAST COMMIT

NaNyrs ago

MAINTAINERS

1

CONTRIBUTORS

0

OPEN ISSUES

0

OPEN PRs

0
No data available
VersionTagPublished
1.2.4
latest
2mos ago
No tutorials found
Add a tutorial