节点0.10中未定义Process.domain

Process.domain undefined in Node 0.10

本文关键字:Process domain 未定义 节点      更新时间:2024-04-05

我开始使用域,并尝试了几个Express域中间件包:

https://github.com/brianc/node-domain-middlewarehttps://github.com/baryshev/connect-domain

根据第一个的使用文档,我应该可以访问process.domain,但它是未定义的。

我基本上是在我的app.js 中这样做的

var express = require('express'),
domains = require('express-domain-middleware');
var app = exports.app = express();
app.use(domains);

在控制器中:

exports.index = function(req, res, next) {
  console.log(process.domain);  //undefined
};

什么东西?

在调用index方法之前,您可能需要检查(使用console.log或断点)以确保此行发生:

express.use(domain);

我不知道你的应用程序是如何构建的,但app.use的顺序通常是这样的。

你的app.get('/someurl', yourcontroller.index)应该在app.use(domain)之后。

Ok-这是由于我的中间件中的Mongo调用。显然,所有数据库调用都必须被封装。

var d = domain.create();
d.run(function () {
  client.query('...', d.intercept(function (rows) {
  // ... use rows (note, first arguments error was "intercepted" by the domain)
  }));
});

参考:https://github.com/felixge/node-mysql/issues/308