Node.js - 每个Express请求的域,在另一个域内 [英] Node.js - Domain per Express request, inside another domain

查看:84
本文介绍了Node.js - 每个Express请求的域,在另一个域内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

节点中的错误处理。唉!

我正在尝试布局这样一个基本的Node应用...

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.

这里有一些模拟工人部分的基本代码:

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

我期待的......

我卷曲 http:// localhost:3000 / ,得到一个不错的小'oops'错误,并看到'触发REQUEST错误'和控制台中的错误堆栈。

I curl http://localhost:3000/, get a nice little 'oops' error, and see 'fired REQUEST error' and the error stack in the console.

实际发生的事情......

我得到这两个作为浏览器响应,并在控制台... ...

I get this both as the browser response, and in the console...


ReferenceError:this_function_does_not_exist未定义
at / Stuff / test.js:38:13
回调(/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( /东西/ node_modules /快递/ node_modules /连接/ lib目录/原。 js:190:15)
at next(/Stuff/node_modules/express/node_modules/connect/lib/proto.js:192:9)
at b(domain.js:183:18)$在Domain.run的b $ b(domain.js:123:23)

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)

现在为什么会这样做呢?

Now why would it go and do something like that?

推荐答案

好的,已解决 - Express有一个try / catch块,它首先进入我不存在的函数调用。

Ok, solved - Express has a try/catch block, which is getting to my non-existent function call first.

要让域捕获它,需要将其从当前调用堆栈中取出,例如......

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');
        });

然后域名会抓住它。

这篇关于Node.js - 每个Express请求的域,在另一个域内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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