What is Node.js?

Simple Server

  • Executes JavaScript
  • Event Driven Concurrency 
  • Emits Events 
  • Listens for Responses

Node.js is NOT:

  • NOT written in JS
    • IS a C based interpreter running the V8 JS engine 
  • NOT a framework, more like an API
  • NOT multi-threaded
    • IS single threaded, using events and callbacks 

Pros and Cons

Node.js

Pros

 

  • Write JS on client and Server
  • Fast executions (Node is written in C and uses V8, which is also C) 
  • Asynchronous executions
  • NPM allows for easy module sharing (like Ruby Gems)
  • Great for running development build tasks

Cons

  • Very low-level JS
  • Can be problematic at scale (single threaded)
  • Callback execution flow can be hard to master

Getting Started

$ git clone https://github.com/rileyhilliard/usc-lecture.git usc-node-demo

$ cd usc-node-demo/node-server

nodejs.org

Hello World: I

// server.js

console.log('Helo World!');
console.log('This was logged from the node.js server');
$ node server.js

Hello World: II

$ node hello.js
  • Node.js is a webserver with an API
  • Loading a module allows the use of it's methods
  • node.js http docs: nodejs.org/api/http.html

HTTP Module

// hello.js
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  console.log("I am going to write 'Hello World'");
  res.end('Hello World\n');
  console.log("I wrote 'Hello World'");
}).listen(3000);

console.log('Server running at http://localhost:3000/');

Async Call Stack

$ node async.js
  • Callbacks can be confusing
  • 'seconds()' function runs async
  • 'http.createServer()' fires on page visit
// async.js
var http = require('http');
var counter = 0;

var isThisLast = function(message){
  console.log('3 - isThisLast\'s message is: '+message);
};

var doThisFunction = function(res, func){
  setTimeout(function(){
    console.log('4 - doing doThisFunction');
  },3000)
  func("What's Up Last function being called?!");
};

var seconds = function(){
  var seconds = 0;
  var plural = "";
  setInterval(function(){
    seconds++;
    plural = (seconds > 1 ? "s" : "");
    console.log("It's been "+seconds+" second"+plural+"\n");
  },1000)
};

var doAnotherThing = function(res){
  console.log('2 - doing ANOTHER thing');
  doThisFunction(res, isThisLast);
};

http.createServer(function (req, res) {
  counter++;
  res.writeHead(200, {'Content-Type': 'text/plain'});
  setTimeout(function(){
    doAnotherThing(res);
  }, 0);
  console.log('1 - == END OF STACK == Ran this thing '+counter+' times')
  res.end('Hello World\n');
}).listen(3000);

seconds();
console.log('Server running at http://localhost:3000/');

Write to File 

$ node file.js
  • fs = node File System module
  • File read/write helper
//file.js
var http = require('http');
var fs = require('fs');
var message = "This is the Message that is going";
message += "to be written to a file";

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  fs.writeFile("test.txt", message, function(err) {
    if(err) {
      res.end(err);
    } else {
      console.log("The file was saved!");
      res.end('The file was saved!\n');
    }
  });
}).listen(3000);

console.log('Server running at http://localhost:3000/');

NPM

Node Package Manager

  • Modules
  • Libraries
  • Frameworks
  • Utilities 

Gulp.js

Development Framework

more on this later...

Express.js

Web Framework

Socket.io

Realtime Websockets

Hapi

HTTP Framework

Cheerio

Serverside jQuery

Learn More Node