Advertisement
Guest User

Untitled

a guest
Feb 17th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Use the native libpq bindings, it's faster than the javascript implementation
  2. // This example should fetche the adev_id value from the PostgreSQL database
  3.  
  4. var pg = require('pg').native;
  5. var http = require('http');
  6. var jsdom = require('jsdom');
  7. var fs = require('fs');
  8. //var input = fs.readFileSync("./client.html").toString();
  9.  
  10. var input = "./client.html";
  11.  
  12. var dbUrl = "tcp://hidden";
  13.    
  14. // Create the http server
  15. http.createServer(function (request, response) {
  16.     // Attach listener on end event.
  17.     console.log("Got kicked!");
  18.     request.on('end', function() {
  19.  
  20.         // Create DOM object and manipulate    
  21.         jsdom.env({
  22.             html: input,
  23.             done: function (errors, window) {
  24.                 var $ = window.$;
  25.                 console.log("Created DOM object named window");
  26.                     // Connect and query the database
  27.                     pg.connect(dbUrl, function(err, client) {
  28.                     client.query("SELECT ad FROM alert WHERE ad = 13", function(err, result) {
  29.                         console.log("Queried the database!");
  30.                         // ?? Do we still need the writeHeader(200...) code here now that DOM is being used for local html??
  31.                             $("body").append("<p> Added! courtesy of DOM</p>");
  32.                             $("body").append("<article>ad length is: " + result.rows.length + "</article>");
  33.                             $("body").append("<article>adev_id value is: " + result.rows[0].ad + "</article>");
  34.                         console.log("Should have added the article tags via DOM already!");
  35.                         console.log("ad is: %d", result.rows.length);
  36.                         console.log("ad value is: %d", result.rows[0].ad );
  37.  
  38.                         callback($('body').html());                
  39.  
  40.                     }) // End client.query
  41.                 }) // End pg.connect
  42.             } // End done: function (errors, window)
  43.         }); // End jsdom.env
  44.         }); // End request.on
  45.     }).listen(8080); // End http.CreateServer
  46. pg.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement