diff --git a/Example.md b/Example.md index 0519d1a..982e438 100644 --- a/Example.md +++ b/Example.md @@ -9,17 +9,33 @@ var pg = require('pg'); var conString = "postgres://postgres:1234@localhost/postgres"; var server = http.createServer(function(req, res, next) { + + // get a pg client from the connection pool pg.connect(conString, function(err, client, done) { + var handleError = function(err) { + // no error occurred, continue with the request if(!err) return false; + + // an error occurred, remove the client from the connection pool done(client); next(err); return true; }; + + // record the visit client.query('INSERT INTO visit (date) VALUES ($1)', [new Date()], function(err, result) { + + // handle an error from the query if(handleError(err)) return; + + // get the total number of visits today (including the current visit) client.query('SELECT COUNT(date) AS count FROM visit', function(err, result) { + + // handle an error from the query if(handleError(err)) return; + + // return the client to the connection pool for other requests to use done(); res.writeHead(200, {'content-type': 'text/html'}); res.end('You are visitor number ' + result.rows[0].count);