-
-
Save rla/55f8d030ed5f2f256f47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var pg = require('pg'); | |
var constr = 'postgres://devel:[email protected]/tcc'; | |
var total = 0; | |
setInterval(function() { | |
console.log('Total connect ' + total); | |
}, 2000); | |
var server = http.createServer(function(req, res) { | |
var start = Date.now(); | |
pg.connect(constr, function(err, client, done) { | |
total += Date.now() - start; | |
var handleError = function(err) { | |
if(!err) return false; | |
done(client); | |
res.writeHead(500, {'content-type': 'text/plain'}); | |
res.end('An error occurred'); | |
return true; | |
}; | |
sql = 'SELECT \ | |
pessoa.cod, \ | |
pessoa.nome, \ | |
pessoa.nasc, \ | |
cidade.nome AS cidade \ | |
FROM pessoa, cidade \ | |
WHERE cidade.cod IN (1, 2, 3) \ | |
LIMIT 1000;'; | |
client.query(sql, function(err, result) { | |
if(handleError(err)) return; | |
res.writeHead(200, {'content-type': 'text/html'}); | |
res.write("<table border='1'>"); | |
result.rows.forEach(function (pessoa) { | |
res.write('<tr>'); | |
res.write('<td>' + pessoa.nome + '</td>'); | |
res.write('<td>' + pessoa.nasc.toLocaleDateString() + '</td>'); | |
res.write('<td>' + pessoa.cidade + '</td>'); | |
res.write('</tr>'); | |
}); | |
res.end('</table>'); | |
done(); | |
}); | |
}); | |
}); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment