Last active
August 29, 2015 14:01
-
-
Save FernandoBasso/2a6e5a21a6dd3305cd5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 server = http.createServer(function(req, res) { | |
pg.connect(constr, function(err, client, done) { | |
var handleError = function(err) { | |
if(!err) return false; | |
done(client); | |
res.writeHead(500, {'content-type': 'text/plain'}); | |
console.error(err); | |
res.end('An error occurred: ' + err); | |
return true; | |
}; | |
txtsql = 'SELECT \ | |
pessoa.cod, \ | |
pessoa.nome, \ | |
pessoa.nasc, \ | |
cidade.nome AS cidade \ | |
FROM pessoa, cidade \ | |
WHERE cidade.cod IN ($1, $2, $3) \ | |
LIMIT 1000;'; | |
var query = client.query({ | |
text: txtsql, | |
values: [1, 2, 3], | |
name: 'myquery' | |
}); | |
res.writeHead(200, {'content-type': 'text/html'}); | |
query.on('row', function(pessoa) { | |
res.write("<table border='1'>"); | |
res.write("<tr>"); | |
res.write("<td>" + pessoa.nome + "</td>"); | |
res.write("<td>" + pessoa.nasc.toLocaleString() + "</td>"); | |
res.write("</tr>"); | |
done(); | |
}); | |
query.on('end', function() { | |
//done(); | |
res.end("</table>"); | |
}); | |
}); | |
}); | |
server.listen(8080); | |
Okay, there was a bug, this should work:
var http = require('http');
var pg = require('pg');
var constr = 'postgres://devel:[email protected]/tcc';
var server = http.createServer(function(req, res) {
if (req.url !== '/') {
res.statusCode = 404;
return res.end('not found');
}
pg.connect(constr, function(err, client, done) {
if(err) {
console.error(err);
done(client);
res.statusCode = 500;
return res.end('An error occurred: ' + err.message);
}
var txtsql = '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({
text: txtsql,
values: [1, 2, 3],
name: 'myquery'
}, function (err, result) {
done(err ? client : false);
if (err) {
console.error(err);
res.statusCode = 500;
return res.end(err.message);
}
res.writeHead(200, {'content-type': 'text/html'});
var html = result.rows.reduce(function (html, row) {
return html += "<tr><td>" + row.nome + "</td><td>" + row.nasc.toLocaleString() + "</td></tr>");
}, "<table border='1'>");
res.end(html + "</table>");
});
});
});
server.listen(8080);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, it seems running
solves the gyp problem. I still get the first problem.