问题描述
我正在尝试在docker-compose中运行的一个容器中运行expressJS;当我持续按CMD + R刷新着陆页(大约3到4秒钟)时,它会显示错误:套接字挂起"并损坏.这是错误
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 | }
我已经花了30多个小时,但仍然对此一无所知什么可能是错误的... ???
I've spent more than 30 hours on this but still have no clue on this What can be possibly wrong...???
这是源代码
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
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
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"
}
}
推荐答案
我在您的代码中看到的一个问题是,对/
的请求将被代理,但是整个中间件链仍将被执行的要求.这可能不是您想要的,这可能是在代理请求完成处理之前结束tcp连接的原因.
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.
在使用express时,我建议使用 http-proxy-middleware
代替 http-proxy
,因为它很容易集成为Express中间件:
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}));
...
这篇关于/node_modules/http-proxy/lib/http-proxy/index.js:120;错误:套接字挂断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!