Skip to content

Commit

Permalink
Add the property of _res_data to http.ServerResponse when proxy r…
Browse files Browse the repository at this point in the history
…equest error
  • Loading branch information
yunnysunny committed Sep 25, 2020
1 parent 1760b09 commit b57f079
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog.md
@@ -1,3 +1,7 @@
# v0.4.0
## Add
1. Add the property of `_res_data` to `http.ServerResponse` when come across proxy request error, such as the request is timeout or the backend is broken.

# v0.3.0
## Add
1. Add the parameter of `agent` to enable `keep-alive` in http request.
Expand Down
16 changes: 12 additions & 4 deletions lib/proxy.js
Expand Up @@ -69,17 +69,25 @@ const checkUrl = function(req, urlsToProxy, urlKeys, callback) {
* @property {http.Agent=} agent The instance of the class of http.Agent.
*/

const _sendStatus = function(res, statusCode, message) {
res._res_code = message;
res.status(statusCode).end(message);
};

const DEFAULT_INIT_CONFIG = {
// eslint-disable-next-line no-unused-vars
onError: function(err, agentUrl) {

},
dataPrepare: function(data) {
return data;
},
// eslint-disable-next-line no-unused-vars
headerPrepare: function(req) {
return {};
},
beforeRequest: function() {},
afterRequest: function() {},
timeout: 10000,
jsonDisabled: false
};
Expand Down Expand Up @@ -119,11 +127,11 @@ const _doProxy = function(options, agentUrl, req, res) {
proxyReq.setNoDelay(true);
proxyReq.setTimeout(timeout, function() {
proxyReq.abort();
res.status(504).end('proxy ' + agentUrl +' timeout');
_sendStatus(res, 504, 'proxy ' + agentUrl +' timeout');
});
proxyReq.on('error', function(err) {//TODO check
slogger.error('proxy ' + agentUrl +' error',err);
res.status(500).end('proxy ' + agentUrl +' error');
_sendStatus(res, 500, 'proxy ' + agentUrl +' error');
});

req.pipe(proxyReq);
Expand Down Expand Up @@ -178,7 +186,7 @@ const _doProxy = function(options, agentUrl, req, res) {
apiGateWayReq.on('error', function(err) {
slogger.error('request proxy http interface failed', err, reqOptions);
if (err.code === 'ESOCKETTIMEDOUT' || err.code === 'ETIMEDOUT' ) { //request timeout
return res.status(504).end('proxy ' + agentUrl +' timeout');
return _sendStatus(res, 504, 'proxy ' + agentUrl +' timeout');
}
onError(err, agentUrl);
});
Expand All @@ -204,7 +212,7 @@ module.exports = function doProxy(options) {
if (err) {
const path = req.path;
slogger.error('proxy ' + path +' failed:', err);
return res.status(502).end('proxy ' + path +' failed: ' + err);
return _sendStatus(res, 502, 'proxy ' + path +' failed: ' + err);
}
if (!backUrl) {
return next();
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "http-request-proxy",
"version": "0.3.0",
"version": "0.4.0",
"description": "The middleware for proxying request in express",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit b57f079

Please sign in to comment.