如何在一个域或一个trycatch中包装每个express js请求 [英] How can I wrap every express js request in a domain or trycatch

查看:135
本文介绍了如何在一个域或一个trycatch中包装每个express js请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将 c $ c>或 trycatch 请参阅这里的trycatch信息

我正在尝试创建一个捕获所有的(快速错误处理程序中间件不捕获异步调用),以确保我错过的任何错误被处理与500被发送给用户。

I am trying to create a 'catch all' of sorts (the express error handler middleware does not catch async calls) to be sure any errors I miss are handled with a 500 being sent to the user.

如果你有一个异步函数调用(例如process.nextTick()),那么它将超出明确的错误处理程序的范围,从而完全杀死过程。因此,使用快速错误处理程序在所有情况下都不起作用。

If you have an asynchronous function call (eg. process.nextTick()), then it will be outside the scope of express' error handler, thus killing the process entirely. Thus, using the express error handler will not work in all cases.

推荐答案

Express已经有错误处理程序实现。它继承自连接。要使用它,您需要将其添加为最后一个中间件点(最后一个app.use(...)调用)。例如:

Express already have error handler implementation. It inherit it from connect. To use it you need to add it as the last middleware point (last app.use(...) call). For example:

var express = require('express')
  , app = express();

app.use(app.router);
app.use(express.errorHandler());

// app.get(...), app.post(...), app.listen(...), etc.

如果要使用简单的500响应代码处理所有错误,您可以替换 express.errorHandler()与你自己的功能。在这种情况下,您的代码将如下所示:

If you want to handle all errors with simple 500 response code, you could replace express.errorHandler() with you own function. In that case your code will looks like:

var express = require('express')
  , app = express();

app.use(app.router);
app.use(function(err, req, res, next){
  if (!err) return next();
  res.send(500);
});

// app.get(...), app.post(...), app.listen(...), etc.

有关这种方式的更多信息可以在表达错误示例代码中的注释

More information about that way could be found in express error example comments in code

更新

当然,您可以为每个请求使用域。您可以单独包装每个请求或使用路由器的包装来处理所有异常。代码如下:

Of course you could use domain for each request. You could wrap each request separately or use wrapping for router to handle ALL exceptions. Code is following:

var express = require('express')
    , http = require('http')
    , app = express()
    , domain = require('domain');

//app.use(app.router);
app.use(function(req, res, next){
    var d = domain.create();
    d.on('error', function(er) {
        console.log('error, but oh well', er.message);
        res.send(500);
    });

    // explicitly add req and res
    d.add(req);
    d.add(res);

    d.run(function() {
        app.router(req, res, next);
    });
});

app.get('/', function(req,res){
    process.nextTick(function(){
        throw new Error('Check Error');
    });
});

http.createServer(app).listen(3000, function(){
    console.log('Express server listening on port 3000');
});

!!但是,请不要在生产中使用。其原因在于JS如何投掷工作。这肯定会导致您的应用程序泄漏,并使其更加不稳定。您可以使用这种错误处理来实现关闭的自定义算法(例如关闭已经打开的连接)。有关域的正确使用的更多信息,请参见文档

为了监控泄漏,您可以使用这篇文章

To monitor the leaking you could use the technique from this article.

更新2

我只是不能离开这个没有完成。 trycatch 代码:

I just can't leave this not finished. trycatch code:

var express = require('express')
    , http = require('http')
    , app = express()
    , domain = require('domain')
    , trycatch = require('trycatch');

//app.use(app.router);
app.use(function(req, res, next){
   trycatch(function(){
           app.router(req, res, next);
       }, function(er){
           console.log(er.message);
           res.send(500);
       });
});

app.get('/', function(req,res){
    process.nextTick(function(){
        throw new Error('Check Error');
    });
});

http.createServer(app).listen(3000, function(){
    console.log('Express server listening on port 3000');
});

我已经查看了 trycatch 的来源,没有任何魔法。它仍然是泄漏的原因。 trycatch 下。

I had review the source of trycatch and there was no any magic. It still be cause of leaks. trycatch has domain under the hood.

这篇关于如何在一个域或一个trycatch中包装每个express js请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆