Skip to content

Commit 2ae1e03

Browse files
klimashkinchimurai
authored andcommitted
Recipe for delaying request/response (chimurai#84)
1 parent 679526e commit 2ae1e03

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

recipes/delay.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Delay proxied request/response
2+
3+
Sometimes we need the ability to delay request to backend server or response from it back to client to test long execution time of particular url or all of them. With DevTool's [network throttling](https://developers.google.com/web/tools/chrome-devtools/profile/network-performance/network-conditions?hl=en) we can test slowdown of all request, not separately.
4+
But we can handle each request individually via our proxy, and add delay to its execution time.
5+
6+
Let's assume that we want slow down the access to backend's `/api/get-me-something` resource. Delay request time by 2 seconds and increase response time by 5 seconds.
7+
8+
For achieving it just put additional route handler to your app before proxy handler:
9+
10+
```javascript
11+
const myProxy = proxy({
12+
target: 'http://www.example.com',
13+
changeOrigin: true
14+
});
15+
const proxyDelay = function (req, res, next) {
16+
if (req.originalUrl === '/api/get-me-something') {
17+
// Delay request by 2 seconds
18+
setTimeout(next, 2000);
19+
20+
// Delay response completion by 5 seconds
21+
const endOriginal = res.end;
22+
res.end = function (...args) {
23+
setTimeout(function () {
24+
endOriginal.apply(res, args);
25+
}, 5000);
26+
};
27+
} else {
28+
next();
29+
}
30+
};
31+
32+
app.use('/api', proxyDelay, myProxy);
33+
```
34+
35+
And you will see result in devtools similar to this:
36+
37+
![http-proxy-delay](https://cloud.githubusercontent.com/assets/576077/15839924/49ebe256-2bfb-11e6-8591-ef0101670885.png)

0 commit comments

Comments
 (0)