【发布时间】:2014-01-21 14:39:22
【问题描述】:
我正在尝试使用 postgresql 作为后端编写 expressjs 服务器。每个请求首先调用pg.connect 以获取池连接(client),以及在不再需要连接时将其返回池的方法(done)。例如:
function dbConnect(req, res, next) {
if (res.locals.pgCtx) {
next();
return;
}
pg.connect(dbConn, function (err, client, done) {
if (err) {
res.send(500, err.message);
} else {
app.locals.pgCtx = res.locals.pgCtx = {
client: client,
done: done
};
next();
}
});
}
app.use(allowCrossDomain);
app.use(express.methodOverride());
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.logger());
app.use(passport.initialize());
app.use(express["static"](webRootDir));
app.use(dbConnect); // <--------------
app.use(authenticate);
app.use(app.router);
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('view engine', 'jade');
app.set('views', webRootDir);
app.engine("jade", jade.__express);
indexHandlers = [fetchConstData, function (req, res) {
res.render(templatesDir + 'index', {
constData: app.locals.constData,
env: app.get('env'),
user: req.user,
admin: isAdmin(req.user.role),
host: req.host
});
}];
app.get('/', indexHandlers);
app.get('/index', indexHandlers);
app.get('/index.html', indexHandlers);
我的问题是,虽然我可以将 dbConnect 作为全局中间件插入到请求的任何其他中间件之前运行,但我还需要能够在所有中间件运行后进行清理,以便将连接返回到游泳池。
理想情况下,无论请求如何结束,都应该有一种方法来指定在所有请求特定的中间件运行后运行的全局中间件——通过以下方式:
res.send(...)- 抛出异常
- 将错误对象传递给
next()
请注意,任何特定于请求的中间件都可以通过这种方式终止链。
目前我只能看到这种方法:
- 通过注册自定义全局错误处理程序中间件而不是
express.errorHandler来挂钩负面结果。 - 将
res对象中的res.send方法替换为自定义版本,该版本首先将连接返回到池,然后继续执行原始res.send实现。
所有这些都带有强烈的黑客气味。我想做对了,所以我想问有没有办法注册请求清理中间件之类的东西?
编辑
静态内容处理程序必须移到dbConnect 中间件上方,否则我们会泄漏数据库连接,直到没有更多可用连接并且服务器无法提供任何服务,因为dbConnect 永远不会返回等待释放连接。
【问题讨论】:
-
这对我来说真的很糟糕。有人吗?