I have implemented a HTTP-proxy server app in Node.js, using the http-proxy
module.
Occasionally the App crashes and according to the logfile it seems like it is caused by a connection reset exception, thrown by the http-proxy module
Logile:
2021-11-01T12:50:57.006759+00:00 heroku[web.1]: Process exited with status 1
2021-11-01T12:50:57.046446+00:00 heroku[web.1]: State changed from up to crashed
2021-11-01T12:50:56.840974+00:00 app[web.1]: /app/node_modules/http-proxy/lib/http-proxy/index.js:120
2021-11-01T12:50:56.840981+00:00 app[web.1]: throw err;
2021-11-01T12:50:56.840982+00:00 app[web.1]: ^
2021-11-01T12:50:56.840982+00:00 app[web.1]:
2021-11-01T12:50:56.840983+00:00 app[web.1]: Error: socket hang up
2021-11-01T12:50:56.840983+00:00 app[web.1]: at connResetException (internal/errors.js:609:14)
2021-11-01T12:50:56.840983+00:00 app[web.1]: at TLSSocket.socketCloseListener (_http_client.js:401:25)
2021-11-01T12:50:56.840984+00:00 app[web.1]: at TLSSocket.emit (events.js:326:22)
2021-11-01T12:50:56.840984+00:00 app[web.1]: at net.js:675:12
2021-11-01T12:50:56.840985+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7) {
2021-11-01T12:50:56.840985+00:00 app[web.1]: code: 'ECONNRESET'
2021-11-01T12:50:56.840985+00:00 app[web.1]: }
2021-11-01T12:50:56.849770+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-11-01T12:50:56.849910+00:00 app[web.1]: npm ERR! errno 1
2021-11-01T12:50:56.852684+00:00 app[web.1]: npm ERR! dexautheu@1.0.0 start: `node dexAuthEU.js`
2021-11-01T12:50:56.852724+00:00 app[web.1]: npm ERR! Exit status 1
2021-11-01T12:50:56.852780+00:00 app[web.1]: npm ERR!
2021-11-01T12:50:56.852819+00:00 app[web.1]: npm ERR! Failed at the dexautheu@1.0.0 start script.
2021-11-01T12:50:56.852853+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-11-01T12:50:56.861038+00:00 app[web.1]:
2021-11-01T12:50:56.861180+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-11-01T12:50:56.861228+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-11-01T12_50_56_853Z-debug.log
I am quite new to java script but I recon that I need to catch this exception in my code and handle it, but the problem is I really don't know where in my app I should put it. I already use try/catch to handle exceptions from the JSON parser. Here is a sample of my app:
var httpProxy = require('http-proxy');
const port = process.env.PORT;
var proxy = httpProxy.createServer({
target: '<target-server>',
secure: false,
followRedirects: true,
autoRewrite: true,
changeOrigin: true,
selfHandleResponse: true
}).listen(port, function() {
console.log(`Server running on port ${port}`);
});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
var Account = '';
Account = proxyReq.getHeader('account');
console.log('Request from: ', Account);
});
proxy.on('proxyRes', function (proxyRes, req, res) {
var body = '';
var response = '';
proxyRes.on('data', function (chunk) {
body += chunk;
});
proxyRes.on('end', function () {
try {
response = JSON.parse(body);
if (response.hasOwnProperty('Code')) {
console.log('Error Message: ', JSON.stringify(response['Code']));
if (response['Code'] == 'SSO_AuthenticateAccountNotFound' || response['Code'] == 'SSO_AuthenticatePasswordInvalid') {
res.writeHead(401);
res.end(JSON.stringify({SessionID: '', ErrorMsg : response['Code'], ErrorCode : 401}));
}
} else {
console.log('SessionID: ', response);
res.end(JSON.stringify({SessionID : response, ErrorMsg : 'OK', ErrorCode: 200}));
}
}
catch (error) {
console.log('JSON error: ' , error);
}
});
});
I saw an example somewhere in which they defined the error handling here:
var proxy = httpProxy.createServer({
target: '<target-server>',
secure: false,
followRedirects: true,
autoRewrite: true,
changeOrigin: true,
selfHandleResponse: true
}).listen(port, function(err) {
if (err) {
console.log('Error serving https proxy request: %s', req);
}
console.log(`Server running on port ${port}`);
});
Would this be an appropriate solution? Or should I have a different strategy? Thank you in advance.