Express 继续获取并发布/socket.io/?EIO=3&;运输=投票及;t=

Express 继续获取并发布/socket.io/?EIO=3&;运输=投票及;t=,express,nginx,socket.io,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/?E

我在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客户端和服务器配置正确。 请参阅有关如何设置客户端和服务器的说明

默认情况下,当连接失败时,客户端会不断尝试无限次重新连接, 通过设置 选项

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

const socket = io(serverURL, { reconnectionAttempts: 10 });
另外,为了最小化日志的大小,当您在
生产中时,不要记录整个错误
对象
, 您可以更新错误处理程序,只记录相关的详细信息

// 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将使用其默认处理程序

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

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

发件人:

这可能是因为:

  • 您没有错误处理程序中间件,而Express default错误处理程序正在记录所有错误堆栈跟踪
  • 您有错误处理程序,但它正在记录所有错误堆栈跟踪
  • 首先,确保您正在生产环境中运行,以便不会将错误堆栈跟踪发送到客户端

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

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

    app.use(函数(请求、恢复、下一步){
    常量错误=新错误(“未找到”);
    err.status=404;
    res.locals.exception=err;
    next();
    });
    //自定义错误处理程序
    应用程序使用(功能(请求、恢复){
    设err=res.locals.exception;
    //如果需要,请在此处添加最小日志记录
    如果(err.message==“未找到”){
    返回res.status(404.send();
    }否则{
    返回res.status(500.send();
    }
    //或
    返回res.status(err.status).send(err.message);
    });
    
    或者编写自己的实际自定义错误处理程序中间件(确保将其放在404错误处理程序之后)

    app.use(函数(请求、恢复、下一步){
    常量错误=新错误(“未找到”);
    err.status=404;
    下一个(错误);
    });
    //注意,第一个参数是错误
    应用程序使用(功能(错误、请求、恢复){
    //如果需要,请在此处添加最小日志记录
    如果(err.message==“未找到”){
    返回res.status(404.send();
    }否则{
    返回res.status(500.send();
    }
    //或
    返回res.status(err.status).send(err.message);
    });
    //--或者如果您使用的是typescript--
    //首先声明错误处理程序
    常量errorHandler:ErrorRequestHandler=(err、req、res、next)=>{
    //如果需要,请在此处添加最小日志记录
    如果(err.message==“未找到”){
    返回res.status(404.send();
    }否则{
    返回res.status(500.send();
    }
    //或
    返回res.status(err.status).send(err.message);
    }; 
    //然后使用它
    应用程序使用(errorHandler);
    
    我一直在使用第一个选项(
    res.locals
    ),因为在Typescript上它不支持错误处理程序中间件。我只是想出来了,把它添加到例子中,从中找到了解决方案