Skip to content

Instantly share code, notes, and snippets.

@rla
Forked from FernandoBasso/prog.cpp
Last active August 29, 2015 14:07
Show Gist options
  • Save rla/55f8d030ed5f2f256f47 to your computer and use it in GitHub Desktop.
Save rla/55f8d030ed5f2f256f47 to your computer and use it in GitHub Desktop.
var http = require('http');
var pg = require('pg');
var constr = 'postgres://devel:1234@127.0.0.1/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