Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso FernandoBasso/nodepgsql.js
Last active Aug 29, 2015

Embed
What would you like to do?
var http = require('http');
var pg = require('pg');
var constr = 'postgres://devel:1234@127.0.0.1/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

This comment has been minimized.

Copy link
Owner Author

FernandoBasso commented Sep 27, 2014

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

This comment has been minimized.

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:1234@127.0.0.1/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
You can’t perform that action at this time.