forked from brianc/node-postgres
- Notifications
You must be signed in to change notification settings - Fork 0
Example
ljfh001 edited this page Aug 6, 2011
·
16 revisions
You'll need to first "create table visit (date date)" in your postgres database
varsys=require('sys');varhttp=require('http');varpg=require('pg').native;varconnectionString="postgres://postgres:abcd@localhost:5432/postgres";varserver=http.createServer(function(req,res){varstart=newDate();if(req.url!="/"){res.writeHead(404);returnres.end("404'd")}varafter=function(callback){returnfunction(err,queryResult){if(err){res.writeHead(500,{"Content-Type" : "text/plain"});returnres.end("Error! "+sys.inspect(err))}callback(queryResult)}}pg.connect(connectionString,after(function(client){client.query("SELECT COUNT(date) as count FROM visit",after(function(result){client.query("SELECT date FROM visit ORDER BY date DESC LIMIT 1",after(function(dateResult){vartext=["<html><head><title>Postgres Node Hello</title><body>","<p>I have been viewed ",result.rows[0].count," times</p>","<p>Most recently I was viewed at ",(dateResult.rows.length!==0 ? dateResult.rows[0].date : ""),"</p>",'you can view the source <a href="http://github.com/brianc/node-postgres">on github</a>','<p>this page took ~',(newDate())-start,' milliseconds to render</p>',"</body></html>"].join('')res.writeHead(200,{"Content-Type": "text/html"})res.end(text);client.query('INSERT INTO visit(date) VALUES($1)',[newDate()])}))}))}))})server.listen(3001)