Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Use the native libpq bindings, it's faster than the javascript implementation
- // This example should fetche the adev_id value from the PostgreSQL database
- var pg = require('pg').native;
- var http = require('http');
- var jsdom = require('jsdom');
- var fs = require('fs');
- //var input = fs.readFileSync("./client.html").toString();
- var input = "./client.html";
- var dbUrl = "tcp://hidden";
- // Create the http server
- http.createServer(function (request, response) {
- // Attach listener on end event.
- console.log("Got kicked!");
- request.on('end', function() {
- // Create DOM object and manipulate
- jsdom.env({
- html: input,
- done: function (errors, window) {
- var $ = window.$;
- console.log("Created DOM object named window");
- // Connect and query the database
- pg.connect(dbUrl, function(err, client) {
- client.query("SELECT ad FROM alert WHERE ad = 13", function(err, result) {
- console.log("Queried the database!");
- // ?? Do we still need the writeHeader(200...) code here now that DOM is being used for local html??
- $("body").append("<p> Added! courtesy of DOM</p>");
- $("body").append("<article>ad length is: " + result.rows.length + "</article>");
- $("body").append("<article>adev_id value is: " + result.rows[0].ad + "</article>");
- console.log("Should have added the article tags via DOM already!");
- console.log("ad is: %d", result.rows.length);
- console.log("ad value is: %d", result.rows[0].ad );
- callback($('body').html());
- }) // End client.query
- }) // End pg.connect
- } // End done: function (errors, window)
- }); // End jsdom.env
- }); // End request.on
- }).listen(8080); // End http.CreateServer
- pg.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement