How to use Node.js 0.8.x domains with express?

后端 未结 5 586
旧时难觅i
旧时难觅i 2021-02-01 19:34

How can I create Express/Connect middleware which wrap each request in its own domain?

5条回答
  •  别那么骄傲
    2021-02-01 19:53

    I've had good luck replacing the stock

    var app = express.createServer();
    

    with:

    var domainCreate = require('domain').create;
    var app = express.createServer(function (req, res, next) {
        var domain = domainCreate();
        domain.run(next);
    });
    

    Then in your middleware you can add properties to process.domain or add additional error handling.

提交回复
热议问题