Node.js

  1. Overview
    1. Node.js is a popular open-source, cross-platform JavaScript runtime that runs outside the web browser
    2. Created by Ryan Dahl in 2009
    3. Can be used for lots of things including web servers
    4. Node.js applications are written in JavaScript and executed with Google V8 JavaScript engine
    5. High performance due to its event-driven architecture (use of callbacks) which can handle millions of concurrent requests on a single process
  2. Install and run
    1. Download Node.js for your OS
    2. Windows installer will create a node.js command prompt
    3. Run node's CL interpreter
      C:\temp>node
      > console.log("Hello Node!");
      Hello Node!
      
    4. Run node app in file test.js
      C:\temp>node test.js
      Hello Node!
      
  3. Modules
    1. Node uses the CommonJS specification to declaring external modules in JavaScript apps
    2. Every file gets a module.exports object that is used to expose objects
    3. Example of exporting a function
      // hello.js
      var sayHello = function() {
          return 'Hello Node';
      };
      
      // Export object "hello" that is
      // the same as sayHello()
      module.exports.hello = sayHello;
      
    4. require() imports objects from module.exports
      // Search for hello.js in the same directory
      var imported = require('./hello.js');
      
      console.log(imported.hello());  // Hello Node
      
    5. Sharing variables between modules
      // config.js
      var config = {
      	maxUnits: 100
      };
      
      module.exports = config;
      
      Get access to config's variables:
      // server.js
      var config = require('./config.js');
      console.log(config.maxUnits);  // 100
      
    6. More info: An overview of Node: Modules and npm
  4. Package management
    1. npm is the pre-installed package manager for the Node.js server platform
    2. Over 100,000 Node.js packages available for easy installation
    3. Use npm command-line tool to download and install (Alt-F12 in WebStorm)
    4. Example installing mkdirp package
      $gt; npm install mkdirp
      
    5. Use require function to load the module
      // Search for node_modules/mkdirp.js or
      // node_modules/mkdirp/index.js
      var mkdirp = require('mkdirp');
      
      // Create temp dir
      mkdirp('temp', function(err) {
          if (err)
              console.log(err);
          else
              console.log("dir was created");
      });
      
    6. package.json
      1. File containing metadata about the module
      2. Excerpt from mkdirp's package.json:
        {
          "name": "mkdirp",
          "description": "Recursively mkdir, like `mkdir -p`",
          "version": "0.5.0",
          "author": {
            "name": "James Halliday",
            "email": "mail@substack.net",
            "url": "http://substack.net"
          },
        etc...
        }
        
      3. More details: Specifics of npm's package.json handling
  5. Create simple HTTP server
    1. Respond with fixed content for every request on port 4000
      // server.js
      var http = require('http');
      var server = http.createServer(function(request, response) {
          response.writeHead(200, {'Content-Type': 'text/plain'});
          response.write('Hello, Node!');
          response.end();
      });
      
      server.listen(4000);
      console.log("Server is listening on port 4000");
      
    2. Run server.js and access URL in web browser
      http://localhost:4000/