@@ -21,12 +21,14 @@ const proxy = createProxyMiddleware({
21
21
/**
22
22
* Intercept response and replace 'Hello' with 'Teapot' with 418 http response status code
23
23
**/
24
- onProxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
25
- res .statusCode = 418 ; // set different response status code
26
-
27
- const response = responseBuffer .toString (' utf8' );
28
- return response .replace (' Hello' , ' Teapot' );
29
- }),
24
+ on: {
25
+ proxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
26
+ res .statusCode = 418 ; // set different response status code
27
+
28
+ const response = responseBuffer .toString (' utf8' );
29
+ return response .replace (' Hello' , ' Teapot' );
30
+ }),
31
+ },
30
32
});
31
33
```
32
34
@@ -39,17 +41,19 @@ const proxy = createProxyMiddleware({
39
41
40
42
selfHandleResponse: true , // res.end() will be called internally by responseInterceptor()
41
43
42
- onProxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
43
- // log original request and proxied request info
44
- const exchange = ` [DEBUG] ${ req .method } ${ req .path } -> ${ proxyRes .req .protocol } //${ proxyRes .req .host }${ proxyRes .req .path } [${ proxyRes .statusCode } ]` ;
45
- console .log (exchange); // [DEBUG] GET / -> http://www.example.com [200]
44
+ on: {
45
+ proxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
46
+ // log original request and proxied request info
47
+ const exchange = ` [DEBUG] ${ req .method } ${ req .path } -> ${ proxyRes .req .protocol } //${ proxyRes .req .host }${ proxyRes .req .path } [${ proxyRes .statusCode } ]` ;
48
+ console .log (exchange); // [DEBUG] GET / -> http://www.example.com [200]
46
49
47
- // log complete response
48
- const response = responseBuffer .toString (' utf8' );
49
- console .log (response); // log response body
50
+ // log complete response
51
+ const response = responseBuffer .toString (' utf8' );
52
+ console .log (response); // log response body
50
53
51
- return responseBuffer;
52
- }),
54
+ return responseBuffer;
55
+ }),
56
+ },
53
57
});
54
58
```
55
59
@@ -62,21 +66,23 @@ const proxy = createProxyMiddleware({
62
66
63
67
selfHandleResponse: true , // res.end() will be called internally by responseInterceptor()
64
68
65
- onProxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
66
- // detect json responses
67
- if (proxyRes .headers [' content-type' ] === ' application/json' ) {
68
- let data = JSON .parse (responseBuffer .toString (' utf8' ));
69
+ on: {
70
+ proxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
71
+ // detect json responses
72
+ if (proxyRes .headers [' content-type' ] === ' application/json' ) {
73
+ let data = JSON .parse (responseBuffer .toString (' utf8' ));
69
74
70
- // manipulate JSON data here
71
- data = Object .assign ({}, data, { extra: ' foo bar' });
75
+ // manipulate JSON data here
76
+ data = Object .assign ({}, data, { extra: ' foo bar' });
72
77
73
- // return manipulated JSON
74
- return JSON .stringify (data);
75
- }
78
+ // return manipulated JSON
79
+ return JSON .stringify (data);
80
+ }
76
81
77
- // return other content-types as-is
78
- return responseBuffer;
79
- }),
82
+ // return other content-types as-is
83
+ return responseBuffer;
84
+ }),
85
+ },
80
86
});
81
87
```
82
88
@@ -107,23 +113,25 @@ const proxy = createProxyMiddleware({
107
113
108
114
selfHandleResponse: true , // res.end() will be called internally by responseInterceptor()
109
115
110
- onProxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
111
- const imageTypes = [' image/png' , ' image/jpg' , ' image/jpeg' , ' image/gif' ];
112
-
113
- // detect image responses
114
- if (imageTypes .includes (proxyRes .headers [' content-type' ])) {
115
- try {
116
- const image = await Jimp .read (responseBuffer);
117
- image .flip (true , false ).sepia ().pixelate (5 );
118
- return image .getBufferAsync (Jimp .AUTO );
119
- } catch (err) {
120
- console .log (' image processing error: ' , err);
121
- return responseBuffer;
116
+ on: {
117
+ proxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
118
+ const imageTypes = [' image/png' , ' image/jpg' , ' image/jpeg' , ' image/gif' ];
119
+
120
+ // detect image responses
121
+ if (imageTypes .includes (proxyRes .headers [' content-type' ])) {
122
+ try {
123
+ const image = await Jimp .read (responseBuffer);
124
+ image .flip (true , false ).sepia ().pixelate (5 );
125
+ return image .getBufferAsync (Jimp .AUTO );
126
+ } catch (err) {
127
+ console .log (' image processing error: ' , err);
128
+ return responseBuffer;
129
+ }
122
130
}
123
- }
124
131
125
- return responseBuffer; // return other content-types as-is
126
- }),
132
+ return responseBuffer; // return other content-types as-is
133
+ }),
134
+ },
127
135
});
128
136
129
137
// http://localhost:3000/wikipedia/en/7/7d/Lenna\_%28test_image%29.png
@@ -146,9 +154,11 @@ const proxy = createProxyMiddleware({
146
154
/**
147
155
* Intercept response and remove the
148
156
**/
149
- onProxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
150
- res .removeHeader (' content-security-policy' ); // Remove the Content Security Policy header
151
- res .setHeader (' HPM-Header' , ' Intercepted by HPM' ); // Set a new header and value
152
- }),
157
+ on: {
158
+ proxyRes: responseInterceptor (async (responseBuffer , proxyRes , req , res ) => {
159
+ res .removeHeader (' content-security-policy' ); // Remove the Content Security Policy header
160
+ res .setHeader (' HPM-Header' , ' Intercepted by HPM' ); // Set a new header and value
161
+ }),
162
+ },
153
163
});
154
164
```
0 commit comments