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



我在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 文档。

默认情况下,当连接失败时,客户端会无限期地尝试重新连接, 您可以通过设置reconnectionAttempts选项。

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

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

此外,为了最小化日志的大小,不要在productionobject记录整个错误, 您可以更新错误处理程序以仅记录相关详细信息

// 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)
})

由于您要将错误传递给next参数,因此它会检测为错误,如果您没有任何错误处理程序,Express 将使用其默认处理程序。

如果你向 next(( 函数传递任何内容(字符串 'route' 除外(,Express 会将当前请求视为错误,并将跳过任何剩余的非错误处理路由和中间件函数。

如果将错误传递给 next(( 并且未在自定义错误处理程序中处理它,则它将由内置错误处理程序处理;错误将与堆栈跟踪一起写入客户端。堆栈跟踪不包括在生产环境中。

寄件人: https://expressjs.com/en/guide/error-handling.html

所以可能是因为:

  1. 您没有错误处理程序中间件,并且 Express 默认错误处理程序正在记录所有错误堆栈跟踪
  2. 你有错误处理程序,但它正在记录所有错误堆栈跟踪。

首先,请确保在生产环境中运行,以便错误堆栈跟踪不会发送到客户端

将环境变量NODE_ENV设置为生产,以在生产模式下运行应用。

如果要将数据(错误(传递到下一个中间件而不将其检测为错误,请将其存储在res.locals中,请在此处阅读更多内容。

app.use(function (req, res, next) {
const err = new Error("Not Found");
err.status = 404;
res.locals.exception = err;
next();
});
// Custom Error handler
app.use(function (req, res) {
let err = res.locals.exception;
// Add Minimum logging here if you want
if(err.message === "Not Found") {
return res.status(404).send();
} else {
return res.status(500).send();
}
// or
return res.status(err.status).send(err.message);
});

或者编写你自己的实际自定义错误处理程序中间件(确保将其放在 404错误处理程序之后(

app.use(function (req, res, next) {
const err = new Error("Not Found");
err.status = 404;
next(err);
});
// Actual Error handler Middleware, notice the first parameter is the error
app.use(function (err, req, res) {
// Add Minimum logging here if you want
if(err.message === "Not Found") {
return res.status(404).send();
} else {
return res.status(500).send();
}
// or
return res.status(err.status).send(err.message);
});
// -- or if you are using typescript --
// declare the error handler first
const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
// Add Minimum logging here if you want
if(err.message === "Not Found") {
return res.status(404).send();
} else {
return res.status(500).send();
}
// or
return res.status(err.status).send(err.message);
}; 
// then use it
app.use(errorHandler);

我一直在使用第一个选项(res.locals(,因为在 Typescript 上它不以某种方式支持错误处理程序中间件。我刚刚想通了,将其添加到示例中,从这个 github 问题中找到了解决方案

相关内容

最新更新