Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso
Last active August 29, 2015 14:01
Show Gist options
  • Save FernandoBasso/2a6e5a21a6dd3305cd5b to your computer and use it in GitHub Desktop.
Save FernandoBasso/2a6e5a21a6dd3305cd5b to your computer and use it in GitHub Desktop.
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);
@FernandoBasso
Copy link
Author

Well, it seems running

sudo npm config set python /usr/bin/python2.7 -g

solves the gyp problem. I still get the first problem.

@gildean
Copy link

gildean commented Nov 8, 2014

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