简体   繁体   English

Node.js - 每个Express请求的域,在另一个域内

[英]Node.js - Domain per Express request, inside another domain

Error handling in Node. 节点中的错误处理。 Argh! 哎呀!

I'm trying to layout a basic Node app like this... 我正在尝试布局像这样的基本节点应用程序...

Cluster -> Worker -> Server Domain -> Express Request Domain 群集 - >工作人员 - >服务器域 - >快速请求域

So, if an error is thrown 18 layers deep into a call stack because someone misspelled their name on a login form, the entire server doesn't crash. 因此,如果错误被抛出18层深入调用堆栈,因为有人在登录表单上拼错了他们的名字,整个服务器都不会崩溃。

Here's some basic code to simulate the worker part: 这是一些模拟工人部分的基本代码:

var domain, server;

domain = require('domain');
server = domain.create();

server.on('error', function(e) {
    console.log('total meltdown...', e.stack);
});

server.run(function() {

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

    express.configure(function() {

        // Domain on EVERY request
        express.use(function(req, res, next) {
            var d = domain.create();

            d.on('error', function(e) {
                console.log('fired REQUEST error', e.stack);
                next(e);
            });

            d.run(next);

        });

        // Generic error handler
        express.use(function(e, req, res, next) {
            res.status(500);
            res.end('oops');
        });

        // Serve the request with a blatent error
        express.get('/', function(req, res) {

            this_function_does_not_exist();
            res.end('we will never get here');

        });
    });

    // Fire 'er up
    express.listen(3000);
});

What I'm expecting... 我期待的......

I curl http://localhost:3000/ , get a nice little 'oops' error, and see 'fired REQUEST error' and the error stack in the console. 我卷曲http://localhost:3000/ ,得到一个不错的小'oops'错误,并在控制台中看到'触发REQUEST错误'和错误堆栈。

What actually happens... 究竟发生了什么......

I get this both as the browser response, and in the console... 我把它作为浏览器响应和控制台......

ReferenceError: this_function_does_not_exist is not defined at /Stuff/test.js:38:13 at callbacks (/Stuff/node_modules/express/lib/router/index.js:161:37) at param (/Stuff/node_modules/express/lib/router/index.js:135:11) at pass (/Stuff/node_modules/express/lib/router/index.js:142:5) at Router._dispatch (/Stuff/node_modules/express/lib/router/index.js:170:5) at Object.router (/Stuff/node_modules/express/lib/router/index.js:33:10) at next (/Stuff/node_modules/express/node_modules/connect/lib/proto.js:190:15) at next (/Stuff/node_modules/express/node_modules/connect/lib/proto.js:192:9) at b (domain.js:183:18) at Domain.run (domain.js:123:23) ReferenceError:this_function_does_not_exist未在/Stuff/test.js:38:13在param(/ Stuff / node_modules / express / lib)的回调(/Stuff/node_modules/express/lib/router/index.js:161:37)处定义/router/index.js:135:11)在Router._dispatch传递(/Stuff/node_modules/express/lib/router/index.js:142:5)(/ Stuff / node_modules / express / lib / router / index) .js:170:5)在Object.router(/Stuff/node_modules/express/lib/router/index.js:33:10)下一步(/Stuff/node_modules/express/node_modules/connect/lib/proto.js) :190:15)下一个(/Stuff/node_modules/express/node_modules/connect/lib/proto.js:192:9)atb(domain.js:183:18)在Domain.run(domain.js:123) :23)

Now why would it go and do something like that? 现在为什么会这样做呢?

Ok, solved - Express has a try/catch block, which is getting to my non-existent function call first. 好的,已解决 - Express有一个try / catch块,它首先进入我不存在的函数调用。

To have the domain catch it, it needs to be taken out of the current call stack, like... 要让域捕获它,它需要从当前调用堆栈中取出,如...

        process.nextTick(function() {
            this_function_does_not_exist();
            res.end('we will never get here');
        });

THEN the domain will grab it. 然后该域名将抓住它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM