@springernature/backend-proxy
Advanced tools
Comparing version 1.3.2 to 1.4.0
# History | ||
## 1.4.0 | ||
_11/03/20_ | ||
- `interceptErrors` option added. ([joeyciechanowicz](https://github.com/joeyciechanowicz)) | ||
## 1.3.2 | ||
@@ -4,0 +9,0 @@ _26/02/20_ |
{ | ||
"name": "@springernature/backend-proxy", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Proxies frontend requests to a backend and can render the result", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -42,3 +42,3 @@ # backend-proxy | ||
| *Property* | *Description* | *Type* | *Default* | | ||
|-----------------------|----------------------------------------------------------------------------------------|---------|--------------------| | ||
|---|---|---|---| | ||
| `backend` | Backend service to proxy requests to | string | | | ||
@@ -48,2 +48,3 @@ | `requiredContentType` | Backend response content type thats required to allow interception and deserialization | string | `application/json` | | ||
| `changeHost` | Set the `host` header on the backend HTTP request to the backend host | boolean | `false` | | ||
| `interceptErrors` | Should backend responses with HTTP 400 - 599 be intercepted and raised as express errors. | boolean | `false` | | ||
| `key` | The property on the request object that the backend response will be stored under. | string | `backendResponse` | | ||
@@ -50,0 +51,0 @@ |
@@ -12,3 +12,4 @@ const http = require('http'); | ||
requiredContentType: 'application/json', | ||
changeHost: false | ||
changeHost: false, | ||
interceptErrors: false | ||
}; | ||
@@ -28,2 +29,3 @@ | ||
* @param {boolean} [options.changeHost=false] - Should the request to the backend have its host field set to the backend url | ||
* @param {boolean} [options.interceptErrors=false] - Should backend responses with HTTP 400 - 599 be intercepted and raised as express errors | ||
* @returns {function} - An Express middleware | ||
@@ -81,6 +83,16 @@ */ | ||
}); | ||
backendResponse.on('error', error => { | ||
next(error); | ||
}); | ||
} else { | ||
// Pipe it back to the client as is | ||
// We don't have the correct content-type, usually this is because a backend responded with a redirect | ||
// or an error | ||
response.statusCode = backendResponse.statusCode; | ||
// Should we intercept the error and raise it as an express error | ||
if (options.interceptErrors && backendResponse.statusCode >= 400 && backendResponse.statusCode <= 599) { | ||
return next({statusCode: backendResponse.statusCode, backendResponse: backendResponse}); | ||
} | ||
// If it's a redirect we need to rewrite the URL to be relative (to the frontend) | ||
if (backendResponse.statusCode >= 300 && backendResponse.statusCode <= 399 && backendResponse.headers.location) { | ||
@@ -93,2 +105,3 @@ if (backendResponse.headers.location.includes(backendHttpOptions.host)) { | ||
// Proxy the headers and backend response to the client | ||
response.header(backendResponse.headers); | ||
@@ -95,0 +108,0 @@ backendResponse.pipe(response); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17811
203
144