簡體   English   中英

Node.js - 每個Express請求的域,在另一個域內

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

節點中的錯誤處理。 哎呀!

我正在嘗試布局像這樣的基本節點應用程序...

群集 - >工作人員 - >服務器域 - >快速請求域

因此,如果錯誤被拋出18層深入調用堆棧,因為有人在登錄表單上拼錯了他們的名字,整個服務器都不會崩潰。

這是一些模擬工人部分的基本代碼:

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錯誤'和錯誤堆棧。

究竟發生了什么......

我把它作為瀏覽器響應和控制台......

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)

現在為什么會這樣做呢?

好的,已解決 - Express有一個try / catch塊,它首先進入我不存在的函數調用。

要讓域捕獲它,它需要從當前調用堆棧中取出,如...

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

然后該域名將抓住它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM