mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
add code comments
parent
8f96c0e537
commit
87e6a0f011
16
Example.md
16
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user