/node_modules/http-proxy/lib/http-proxy/index.js:120; Error: socket hang up

我们两清 提交于 2021-02-08 08:55:23

问题


I'm running expressJS in one of the containers I'm trying to run in docker-compose; When I keep pressing CMD+R to refresh the landing page (Probably 3~4 seconds), it says "Error: socket hang up" and broken. Here is the error

saml-enabled-reverse-proxy_1  | /node_modules/http-proxy/lib/http-proxy/index.js:120
saml-enabled-reverse-proxy_1  |     throw err;
saml-enabled-reverse-proxy_1  |     ^
saml-enabled-reverse-proxy_1  | 
saml-enabled-reverse-proxy_1  | Error: socket hang up
saml-enabled-reverse-proxy_1  |     at connResetException (internal/errors.js:561:14)
saml-enabled-reverse-proxy_1  |     at Socket.socketCloseListener (_http_client.js:380:25)
saml-enabled-reverse-proxy_1  |     at Socket.emit (events.js:214:15)
saml-enabled-reverse-proxy_1  |     at TCP.<anonymous> (net.js:658:12) {
saml-enabled-reverse-proxy_1  |   code: 'ECONNRESET'
saml-enabled-reverse-proxy_1  | }

I've spent more than 30 hours on this but still have no clue on this What can be possibly wrong...???

Here is the source code

var express = require("express");
var session = require('express-session');
var cookieParser = require('cookie-parser');
var httpProxy = require('http-proxy');
var fs = require('fs');
var saml = require('passport-saml');
var passport = require('passport');

// ServiceProvider will be the load balancer.
let serviceProvider = 'http://opengrok_docker:8080';
let entryPoint = null;
let hackMode = false;
if (process.env.MODE=='TEST') {
  entryPoint = 'http://localhost:8080/x'
} else if (process.env.MODE=='STAGE') {
  entryPoint = 'xxx'
} else if (process.env.MODE=='PROD') {
  entryPoint = 'xxx'
}

// SAML config options here
var samlStrategy = new saml.Strategy({
    entryPoint: entryPoint,
    issuer: 'urn:opengrok',
    identifierFormat: null,
    decryptionPvk: privateKey, //.key version
    validateInResponseTo: false,
    disableRequestedAuthnContext: false,
}, function(profile, done) {
    return done(null, profile);
});

// Passport Middleware Block
passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});
passport.use('samlStrategy', samlStrategy);

let app = express();
let apiProxy = httpProxy.createProxyServer();
app.get('/', <<<<<----- REFRESHING THIS PAGE over seconds got me error;
    function(req, res) {
        apiProxy.web(req, res, {target: serviceProvider});
    }
);

app.use(session({secret: 'secret', resave: false, saveUninitialized: true}));
app.use(passport.initialize());
app.use(passport.session());
// Static blocks
app.use('/img', express.static(__dirname + '/img'));
app.use('/fonts', express.static(__dirname + '/fonts'));
app.use('/js', express.static(__dirname + '/js'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/font-awesome', express.static(__dirname + '/font-awesome'));
app.use('/source/default/img', express.static(__dirname + '/source/default/img'));
app.use('/source/default', express.static(__dirname + '/source/default'));
app.use('/source/js', express.static(__dirname + '/source/js'));
app.use('/source/api/v1/suggest/config', express.static(__dirname + '/source/config'))

// Serve the app on port 443
var server = app.listen(443, function () {
    console.log('Listening on port %d', server.address().port)
});

Dockerfilie

FROM node:12.10.0

COPY package.json .

RUN npm install

COPY . .
CMD [ "node", "--max-old-space-size=8192", "--trace_gc", "src/index.js" ]

package.json

{
  "name": "aop-sp",
  "version": "1.0.0",
  "description": "Art of Possible Service Provider",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "xxx",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "connect-ensure-login": "^0.1.1",
    "cookie-parser": "^1.4.4",
    "express": "^4.17.1",
    "express-session": "^1.16.2",
    "http-proxy": "^1.0.0",
    "passport": "^0.4.0",
    "passport-saml": "^1.1.0"
  },
  "devDependencies": {
    "http-proxy-middleware": "^0.20.0"
  }
}

回答1:


One problem I see with your code is that a request to / will be proxied but also the whole middleware chain will still be executed for the request. This is probably not what you want and could be the reason for ending the tcp-connection before the proxy request has finished processing.

I recommend using http-proxy-middleware instead of http-proxy when using express as it integrates easily as an express middleware:

 const {createProxyMiddleware} = require('http-proxy-middleware');
 ...
 app.get('/', createProxyMiddleware({ target: serviceProvider}));
 ...


来源:https://stackoverflow.com/questions/61019402/node-modules-http-proxy-lib-http-proxy-index-js120-error-socket-hang-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!