How do I use node-postgres in a server?

落爺英雄遲暮 提交于 2019-12-04 01:39:52
Krut

I'm assuming you're using the latest version of node-postgres, in which the connection pooling has been greatly improved. You must now check the connection back into the pool, or you'll bleed the connections:

app.get('/', function (req, res) {
  pg.connect(pgconnstring, function (err, client, done) {
    // do some stuff
    done();
  });
});

As for error handling on a global connection (#2, but I'd use the pool):

client.on('error', function(e){
  client.connect(); // would check the error, etc in a production app
});

The "missing" docs for all this is on the GitHub wiki.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!