Skip to content

Commit

Permalink
chore: text
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarten van Oudenniel committed Nov 20, 2020
1 parent 78f147c commit 48c9f1c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions recipes/async-response.md
Expand Up @@ -12,14 +12,14 @@ const myProxy = createProxyMiddleware({
proxyReq.setHeader('mpth-1', 'da');
},
onProxyRes: async (proxyRes, req, res) => {
const bar = await new Promise((resolve, reject) => {
const da = await new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ wei: 'da' });
resolve({ wei: 'wei' });
}, 200);
});

// add your dynamic header
res.setHeader('mpth-2', bar.wei);
res.setHeader('mpth-2', da.wei);

// now pipe the response
proxyRes.pipe(res);
Expand All @@ -29,7 +29,7 @@ const myProxy = createProxyMiddleware({
app.use('/api', myProxy);
```

There are also cases where you need to modify the request header async, we can achieve this by applying middleware in front of the proxy or by making onProxyReq async as well. Like:
There are also cases where you need to modify the request header async, we can achieve this by applying middleware in front of the proxy. Like:

```javascript
const entryMiddleware = async (req, res, next) => {
Expand All @@ -56,14 +56,14 @@ const myProxy = createProxyMiddleware({
proxyReq.setHeader('mpth-1', req.locals.da);
},
onProxyRes: async (proxyRes, req, res) => {
const bar = await new Promise((resolve, reject) => {
const da = await new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ wei: 'da' });
resolve({ wei: 'wei' });
}, 200);
});

// end:
res.setHeader('mpth-2', bar.wei);
res.setHeader('mpth-2', da.wei);

proxyRes.pipe(res);
},
Expand Down

0 comments on commit 48c9f1c

Please sign in to comment.