• Web Scraping with node.js and cheerio

    Use node.js and the cheerio node module to scrape or extract data content from a web page. We use the HTTP core module in combination with cheerio, which converts an HTTP response into a DOM, which we use to parse and extract data from specific HTML elements.

    var http = require("http");
    var cheerio = require("cheerio");
    
    var server = http.createServer(function(req, res) {
    
      var req_opts = {
        host:"en.wikipedia.org",
        path:"/wiki/London"
      };
      var response_text = "";
    
      // 1. Perform an HTTP request to Wikipedia
      var request = http.request(req_opts, function(resp) {
        if(resp.statusCode != 200) {
          throw "Error: " + resp.statusCode; 
        };
        resp.setEncoding("utf8");
        resp.on("data", function (chunk) {
          response_text += chunk;
        });
        resp.on("end", function() {
    
          // 2. Parse response using cheerio
          $ = cheerio.load(response_text);
    
          // Begin writing our output HTML
          res.writeHead(200, {"Content-Type": "text/html"});
          res.write("<html><head><meta charset='UTF-8' />");
          res.write("</head><body><table>");
    
          // Iterate over TR elements in the Wikipedia infobox
          $("table.geography tr").each(function(tr_index, tr) {
            var th_text = $(this).find("th").text();
            var prop_name
              = th_text.trim().toLowerCase().replace(/[^a-z]/g,"");
    
            // We're only interested in these 3 fields
            if({"country":1,"mayor":1,"elevation":1}[prop_name])
            {
              // 3. Write out our tabulated data
              res.write("<tr><th>" + prop_name + "</th><td>");
              res.write($(this).find("td").text());
              res.write("</td></tr>");
            }
          });
    
          // And... we're done
          res.end("</table></body></html>");
        });
      });
    
      request.on("error", function(e) {
        throw "Error: " + e.message;
      });
    
      request.end();
    
    }).listen(8080);
    27 Jan 2014
  • Dynamically Generate a PNG from Pixmap Array

    Use node.js and the node-png (libpng) module to generate and manipulate a PNG. An RGB data buffer or pixmap is created programmatically and the result written to file as a PNG image.

    var http = require("http");
    var fs = require("fs");
    var Buffer = require("buffer").Buffer;
    var Png = require("png").Png;
    
    var IMAGE_WIDTH = 255;
    var IMAGE_HEIGHT = 255;
    
    var server = http.createServer(function(req, res) {
    
      var rgb_data = new Buffer(IMAGE_WIDTH * IMAGE_HEIGHT * 3);
    
      for(var h = 0; h < IMAGE_HEIGHT; h++)
      {
        for(var w = 0; w < IMAGE_WIDTH; w++)
        {
          var p = h * IMAGE_WIDTH * 3 + w * 3;
          rgb_data[p + 0] = h; // r (0-255)
          rgb_data[p + 1] = 0; // g (0-255)
          rgb_data[p + 2] = w; // b (0-255)
        }
      }
    
      var png = new Png(rgb_data, IMAGE_WIDTH, IMAGE_HEIGHT, "rgb")
    
      fs.writeFile("output.png", png.encodeSync().toString("binary"),
                   "binary", function(err) {
        if(err) { throw err; }
        res.end("PNG image generated");
      });
    
    }).listen(8080);
    25 Jan 2014
  • Update a MongoDB document using node.js and mongojs

    Update a document in a MongoDB database using the mongojs module and node.js.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      var update = {
        // SET model = 'iPad 3' 
        $set: { model: "iPad 3"}
      };
    
      var query = {
        // WHERE model = 'iPad' 
        model: "iPad"
      };
    
      db.products.update(query, update, function(err, result) {
        if(err) { throw err; }
        res.end("<p>Product updated</p>");
      });
    
    }).listen(8080);
    
    1 Sep 2013
  • Remove a MongoDB document using node.js and mongojs

    Remove (delete) a document from a MongoDB database using the mongojs module and node.js.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      var query = {
        // WHERE make = 'Samsung'
        make: "Samsung"
      };
    
      db.products.remove(query, function(err, result) {
        if(err) { throw err; }
        res.end("<p>Product removed");
      });
    
    }).listen(8080);
    1 Sep 2013
  • Insert a Document into MongoDB using node.js and mongojs

    Insert a document into a MongoDB database using the mongojs module under node.js.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      var data = {
        make: "Nokia",
        model: "Lumia"
      };
    
      db.products.insert(data, function(err, result) {
        if(err) { throw err; }
        res.write("<p>Product inserted:</p>");
        res.end("<p>" + result[0].make + " " + result[0].model + "</p>");
      });
    
    }).listen(8080);
    
    1 Sep 2013
  • Getting Started with MongoDB and mongojs

    How to get started with MongoDB under node.js, using the mongojs module.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      db.products.find(function(err, products) {
        if(err) { throw err; }
    
        res.writeHead(200, {"Content-Type": "text/html"});
        res.write("<table>");
    
        products.forEach(function(product) {
          res.write("<tr>");
          res.write("<td>" + product.make + "</td>");
          res.write("<td>" + product.model + "</td>");
          res.write("</tr>");
        });
    
        res.end("</table>");
      });
    
    }).listen(8080);
    1 Sep 2013
  • Generate a UUID using node.js

    Generate RFC4122 version 1 or version 4 UUIDs in node.js.

    var http = require("http");
    var uuid = require("node-uuid");
    
    http.createServer(function(req, res) {
    
      // v1 (time-based)
      res.write("<p>" + uuid.v1() + "</p>");
    
      // v4 (random)
      res.write("<p>" + uuid.v4() + "</p>");
    
      res.end();
    
    }).listen(8080);
    30 Aug 2013