Skip to content
This repository

This code should be able to be copy/pasted to give you a sample app running on your machine

You'll need to first "create table visit (date date)" in your postgres database

var http = require('http');
var pg = require('pg');

var conString = "postgres://postgres:1234@localhost/postgres";

var server = http.createServer(function(req, res) {

  // 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.
      // A truthy value passed to done will remove the connection from the pool
      // instead of simply returning it to be re-used.
      // In this case, if we have successfully, received a client (truthy)
      // then it will be removed from the pool.
      done(client);
      res.writeHead(500, {'content-type': 'text/html'});
      res.end('An error occurred');
      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);
      });
    });
  });
})

server.listen(3001)
Something went wrong with that request. Please try again.