Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.14 KB

README.md

File metadata and controls

50 lines (33 loc) · 1.14 KB

http-proxy-middleware-body

NPM Version License

Get response body when using http-proxy-middleware.

Example

An example with express server.

const express = require('express'),
    {createProxyMiddleware} = require('http-proxy-middleware'),
    getBody = require('http-proxy-middleware-body'),

    app = express();

app.use(createProxyMiddleware('SOME_CONTEXT', {

    // other configs...

    onProxyRes: (proxyRes, req, res) => getBody(res, proxyRes, rawBody => {

        if (!rawBody) {
            return;
        }

        try {

            // if it's a json body
            const body = JSON.parse(rawBody);

            // token expired
            if (body.code === 'TOKEN_EXPIRED_CODE') {
                // remove token...
            }

        } catch (e) {
            // do something...
        }

    })

}));