You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
constmyProxy=proxy({
12
+
target:'http://www.example.com',
13
+
changeOrigin:true
14
+
});
15
+
constproxyDelay=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
+
constendOriginal=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:
0 commit comments