express - 继续获取和发布/socket.io/?EIO=3&transport=polling&t=

标签 express nginx socket.io

我在 ExpressJS 和 NodeJS 中的后端曾经运行良好。我刚刚意识到日志爆炸了磁盘(后端仍然可用),因为当后端打开时,它一直在尝试:

kpi.js GET /socket.io/?EIO=3&transport=polling&t=NBiaEK6
index.js GET /socket.io/?EIO=3&transport=polling&t=NBiaEK6
index.js router.get *
kpi.js POST /socket.io/?EIO=3&transport=polling&t=NBiaER6
index.js POST /socket.io/?EIO=3&transport=polling&t=NBiaER6
Error: Not Found
    at /opt/funfun/app.js:99:13
    at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
    at /opt/funfun/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
    at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
    at /opt/funfun/node_modules/express/lib/router/index.js:635:15
    at next (/opt/funfun/node_modules/express/lib/router/index.js:260:14)
    at /opt/funfun/routes/index.js:18:2
    at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
    at /opt/funfun/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
    at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/opt/funfun/node_modules/express/lib/router/index.js:174:3)
    at router (/opt/funfun/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
    at /opt/funfun/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
    at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
    at /opt/funfun/node_modules/express/lib/router/index.js:635:15
kpi.js GET /socket.io/?EIO=3&transport=polling&t=NBiaF8A
index.js GET /socket.io/?EIO=3&transport=polling&t=NBiaF8A
index.js router.get *
kpi.js POST /socket.io/?EIO=3&transport=polling&t=NBiaFFz
index.js POST /socket.io/?EIO=3&transport=polling&t=NBiaFFz
Error: Not Found
    at /opt/funfun/app.js:99:13
    at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
    at /opt/funfun/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
    at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
    at /opt/funfun/node_modules/express/lib/router/index.js:635:15
    at next (/opt/funfun/node_modules/express/lib/router/index.js:260:14)
    at /opt/funfun/routes/index.js:18:2
    at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
    at /opt/funfun/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
    at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/opt/funfun/node_modules/express/lib/router/index.js:174:3)
    at router (/opt/funfun/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
    at /opt/funfun/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
    at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
    at /opt/funfun/node_modules/express/lib/router/index.js:635:15

kpi.js GET ...index.js GET ... 是我打印的。这是 opt/funfun/app.js 的代码:

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  var err = new Error('Not Found');  // line 99
  err.status = 404;
  next(err);
});

谁知道可能是什么原因?

最佳答案

如果您有一个 socket.io 客户端尝试连接到,则可能会发生这种情况 您的后端,但您的后端没有配置 socket.io 服务器或 未正确配置为接受连接并且您正在记录任何错误 错误处理程序。

确保正确配置了 socket.io 客户端和服务器。 请参阅socket.io docs关于如何设置客户端和服务器。

默认情况下,客户端会在连接失败时无限尝试重新连接, 您可以通过设置来防止它无限轮询服务器 reconnectionAttempts选项。

例如,这将阻止客户端在 10 次尝试失败后轮询服务器

const socket = io(serverURL, { reconnectionAttempts: 10 });

另外,为了尽量减少日志的大小,当您处于生产时,不要记录整个错误object, 您可以更新错误处理程序以仅记录相关详细信息

// catch errors and forward to error handler
app.use(function (req, res, next) {
    ...
    next(err);
});

//In the error handler 
app.use(function (err, req, res, next)  {
    //log only relevant details
    logger.info(err.message)
})

关于express - 继续获取和发布/socket.io/?EIO=3&transport=polling&t=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62581662/

相关文章:

django - Django 在带有 nginx 的 docker 上未使用静态文件

nginx - 通过 openresty 使用 keycloak 对 websocket 进行身份验证

javascript - 是否有通过 websockets 管理资源的既定模式?

javascript - Angular/Socket.io/HTML/从 HTML 中的 <script> 内调用 Controller 的函数

html - 在 Express 中使用 HTML 而不是 Jade

node.js - bcryptjs 中的加密和解密

具有相似前缀的 nginx 位置

security - Node.js + Express.js 用户权限安全模型

node.js - 通过 Sequelize (Express/NodeJS) 访问 mysql 时出错

javascript - 用新传入的数据 react 添加新行