An open API service indexing awesome lists of open source software.

https://github.com/fatalxiao/http-proxy-middleware-body

Get response body when using http-proxy-middleware.
https://github.com/fatalxiao/http-proxy-middleware-body

http-proxy-middleware

Last synced: 12 months ago
JSON representation

Get response body when using http-proxy-middleware.

Awesome Lists containing this project

README

          

# http-proxy-middleware-body

[![NPM Version][npm-image]][npm-url]
[![License][license-image]][npm-url]

[npm-image]: https://img.shields.io/npm/v/http-proxy-middleware-body.svg?style=flat-square
[npm-url]: https://npmjs.org/package/http-proxy-middleware-body
[license-image]: https://img.shields.io/npm/l/http-proxy-middleware-body.svg?style=flat-square

Get response body when using `http-proxy-middleware`.

## Example

An example with `express` server.

```javascript
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...
}

})

}));
```