getting started

Code examples to help you get started with node.js.

There are 3 snippets with the getting started tag

  • Hello World - node.js

    A simple node.js application that responds with an HTML page.

    var http = require("http");
    
    http.createServer(function(req, res) {
      res.writeHead(200, {"Content-Type": "text/html"});
      var html = "<!DOCTYPE html>"
               + "<html><head>"
               + "  <meta charset='utf-8'>"
               + "  <title>Hello World</title>"
               + "</head><body>"
               + "  <h1>Hello World</h1>"
               + "  <p>Simple node.js application</p>"
               + "</body></html>";
      res.end(html);
    }).listen(8080);
    
    console.log("Server listening on port 8080");
    25 Aug 2013
  • Hello World - Express and node.js

    A simple node.js / Express application that serves an HTML page.

    var express = require("express");
    var app = express();
    
    app.get("*", function(req, res) {
      var html = "<!DOCTYPE html>"
               + "<html><head>"
               + "  <meta charset='utf-8'>"
               + "  <title>Hello World</title>"
               + "</head><body>"
               + "  <h1>Hello World</h1>"
               + "  <p>Simple node.js + Express application</p>"
               + "</body></html>";
      res.send(html);
    });
    
    app.listen(8080);
    
    console.log("Server listening on port 8080");
    
    25 Aug 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