add code comments

Sean Lavine 2014-04-07 16:24:48 -07:00
parent 8f96c0e537
commit 87e6a0f011

@ -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);