Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导致uncaughtException/unhandledRejection的Node.js捕获请求?_Node.js - Fatal编程技术网

导致uncaughtException/unhandledRejection的Node.js捕获请求?

导致uncaughtException/unhandledRejection的Node.js捕获请求?,node.js,Node.js,在节点应用程序中,我们正在应用程序条目文件中侦听未处理的异常/拒绝: process.on('unhandledRejection' ... 请求是这样传入的(相同的过程): 有问题吗?我们无法知道到底是哪个请求导致了崩溃。我们是否有办法将一些req请求信息传递回异常处理程序,以便稍后检查日志并重播有问题的请求 编辑:我们正在使用express,崩溃是由与express无关的外部库引起的。我个人喜欢创建一个函数包装器来捕获所有错误并将它们发送到错误中间件: const asyncErrorHa

在节点应用程序中,我们正在应用程序条目文件中侦听未处理的异常/拒绝:

process.on('unhandledRejection'
...
请求是这样传入的(相同的过程):

有问题吗?我们无法知道到底是哪个请求导致了崩溃。我们是否有办法将一些
req
请求信息传递回异常处理程序,以便稍后检查日志并重播有问题的请求


编辑:我们正在使用express,崩溃是由与express无关的外部库引起的。

我个人喜欢创建一个函数包装器来捕获所有错误并将它们发送到错误中间件:

const asyncErrorHandler = fn =>
  function errorHandler (req, res, next, ...args) {
    const fnReturn = fn(req, res, next, ...args)
    return Promise.resolve(fnReturn).catch(next) // Errors are sent to next()
  }
然后,您必须使用错误处理程序包装所有路由:

app.get('/', asyncWrap(async function(req, res, next) {
  const data = await Database.get({id: 1) // Unhandled errors are automatically sent to next()
  if (someCondition) throw new Error(); // This error is automatically sent to next() too
  res.render('index');
}));

要在控制台中获取请求端点,请使用

server.all('/req', async (req, res) => {
    console.log(req.originalUrl);
    // Logic here
}

是的,我们有。谢谢,我更新了问题。根据。请求中的错误由express本身处理,除非您想编写自定义错误处理函数@Nema Ga
server.all('/req', async (req, res) => {
    console.log(req.originalUrl);
    // Logic here
}