won*_*lik 3 javascript node.js
我正在编写小node.js服务器来帮助维护构建机器.它基本上适用于测试人员能够远程丢弃数据库或重启服务器.我有一些关于pg连接的问题.任何人都可以知道为什么在第一次请求后没有关闭它?
var client = new pg.Client(conString);
var server = http.createServer(function (req, res) {
var url = parse(req.url);
if (url.pathname =='/'){
(...)
}else{
var slash_index = url.pathname.indexOf('/',1);
var command = url.pathname.slice(1,slash_index);
if (command =='restart'){
res.write('restarting server please wait');
} else if (command == 'drop-db'){
console.log('drop-db');
client.connect();
console.log('connect');
var query = client.query("select datname from pg_database;", function(err, result) {
if (err) throw err;
console.log('callback');
});
query.on('end', function() {
console.log('close');
client.end();
});
} else{
res.write('unknown command : '+ command);
}
res.write('\n');
res.end();
}
}).listen(5337);
Run Code Online (Sandbox Code Playgroud)
所以我在第一次请求后在屏幕上看到的是:
drop-db
connect
callback
close
Run Code Online (Sandbox Code Playgroud)
很棒但是在接下来的请求后我才会得到
drop-db
connect
Run Code Online (Sandbox Code Playgroud)
在下一个之后我已经得到了一个pg错误
我做错了什么?
编辑:第二次提交后没有错误.第三次出错:
events.js:48
throw arguments[1]; // Unhandled 'error' event
^
error: invalid frontend message type 0
at [object Object].<anonymous> (/home/wonglik/workspace/server.js/node_modules/pg/lib/connection.js:412:11)
at [object Object].parseMessage (/home/wonglik/workspace/server.js/node_modules/pg/lib/connection.js:287:17)
at Socket.<anonymous> (/home/wonglik/workspace/server.js/node_modules/pg/lib/connection.js:45:22)
at Socket.emit (events.js:88:20)
at TCP.onread (net.js:347:14)
Run Code Online (Sandbox Code Playgroud)
我认为这与旧的仍然开启时打开新连接有关.
编辑2:
我查了postgres日志:
第二次请求后:
2012-03-13 09:23:22 EET LOG: invalid length of startup packet
Run Code Online (Sandbox Code Playgroud)
第三次请求后:
2012-03-13 09:24:48 EET FATAL: invalid frontend message type 0
Run Code Online (Sandbox Code Playgroud)
看起来客户端(pg.Client)声明在请求范围之外,这可能是您的问题.从代码片段中很难说,但看起来您可能会遇到范围问题以及异步回调控制流如何工作,例如在回调仍在IO队列中时调用res.end().这对节点来说是完全合法的,只是不确定这是你的意图.
最好使用返回客户端的pg.connect.请参阅https://github.com/brianc/node-postgres/wiki/pg
var pg = require('pg');
var server = http.createServer(function (req, res) {
var url = parse(req.url);
if (url.pathname =='/'){
(...)
}else{
var slash_index = url.pathname.indexOf('/',1);
var command = url.pathname.slice(1,slash_index);
if (command =='restart'){
res.write('restarting server please wait');
} else if (command == 'drop-db'){
console.log('drop-db');
pg.connect(conString, function(err, client) {
console.log('connect');
var query = client.query("select datname from pg_database;", function(err, result) {
if (err) throw err;
console.log('callback');
});
query.on('end', function() {
console.log('close');
// client.end(); -- not needed, client will return to the pool on drain
});
});
} else{
res.write('unknown command : '+ command);
}
// these shouldn't be here either if you plan to write to res from within the pg
// callback
res.write('\n');
res.end();
}
}).listen(5337);
Run Code Online (Sandbox Code Playgroud)