INTRODUCTION TO NODE.JS & EXPRESS.JS

Travis Chase

Team Architect, Web Apps
Bluehost

Mark Calkins

VP Product
Bluehost

slides url:  slides.com/markcalkins/nodejs-intro

Node.js          Ruby on Rails          Python Django          PHP Laravel

WHAT IS / WHY NODE.JS?

  • Server platform built on Chrome’s JavaScript V8 engine

  • V8 is probably the fastest dynamic language interpreter around

  • Node’s i/o facilities are really light weight

written by Ryan Dahl

DAWN OF ENTERPRISE JAVASCRIPT

Forrester Research:  Node.js is setting the stage for the “the biggest shift in enterprise development in more than a decade.”

CASE STUDY:   PAYPAL

  • Enterprise applications built on Java

  • Started a pilot project to evaluate Node

  • Put 2 developers on the pilot vs 12 Java developers

  • Ended up far exceeding everyone’s expectations

    • 7400 vs 18600 lines of code

    • Easier to read, easier to maintain

    • Created clean boundaries between UI, app logic and services

    • Node makes it easy to use the UI for experimentation

  • 67 Node.js projects now in development in 18 months

CASE STUDY:  WALMART

  • Black Friday traffic represents 70% of their annual website traffic

  • Moved all their web servers over to Node

  • Handled the Black Friday traffic without a hitch

  • Now doubling their Node.js development team from 40 to 80 developers

OTHER ENTERPRISES

who made the move to Node

WHAT ENTERPRISES SAY

Node.js powers our web applications and has allowed our teams to move much faster in bringing their designs to life.

Jeff Harrell - Dir Engineering, PayPal

Node’s evented I/O model freed us from worrying about locking and concurrency issues that are common with multithreaded async I/O

Subbu Allarmarju - Principal Member, eBay

On the server side, our entire mobile software stack is completely built in Node. One reason was scale. The second is Node showed us huge performance gains.

Kiran Prasad - Mobile Development Lead, LinkedIn

WHAT TO USE NODE FOR

  • Ideal for building fast, scalable real-time applications

  • Push technology over WebSockets
    (over port 80!)

  • Handles two-way connections extremely well

WHAT ARE THE TOP USE CASES?

SAMPLE APPLICATIONS

  • Chat server

  • API on top of object store like MongoDB

  • Queueing inputs to a db when receiving large amounts of concurrent data — such as real-time analytics

  • Server-side proxy (like NGINX)

  • Use Node stack for single page app dev

  • Excellent for prototyping!

WHAT NOT TO USE NODE FOR

  • CPU-intensive applications

  • Server application that works with a relational DB

  • Any application where a synchronous processing model is better suited

NODE.JS ARCHITECTURE

CALLBACK FUNCTIONS

  • Creating asynchronous non-blocking code in Node is done with Callback functions

getStuff(inputParam, function(error, results) {
   if error is undefined
   do something with the results
}); 

NODE AS A WEB SERVER

  • Built-in HTTP server

var http = require('http’);
var server = http.createServer(function(req, res) {
   res.write('Hello World') 
   res.end() 
});
 
server.listen(9000, function(){
   console.log('listening on port 9000') 
}); 

NODE.JS SAMPLE APP

NODE MODULES

  • Modules allow you to bring in external functionality into your Node app

  • Modules can export variables, functions and objects

  • Three main sources of Node modules

    • Node’s built-in modules — fs, http, os

    • Third party modules from npm

    • Creating your own modules

  • Modules are a great way to organize your code

USING MODULES

  • Create a function, make it available with either exports or module.exports

  • Use it with require

// create module hello.js
var hello = function(){
   console.log('hello!') 
}; 
module.exports = hello;

// use the module in your main server file
var hello = require('./hello');
hello();
  

USING MODULES

  • Express.js

  • Socket.io

  • Jade

  • Redis

  • MongoDB

  • Mongoose —
    MongoDB ORM

  • Underscore

  • Async

  • Passport — auth

  • Method-override

  • Helmet — security

  • Request — http client

  • Restify — REST APIs

  • Forever — keep Node server running
  • Enables real-time bidirectional event-based communication
  • Great for IM & chat apps, real-time analytics and concurrent document editing
  • socket.io — 1.2 is available now

SOCKET.IO

SOCKET.IO SAMPLE APP

SINATRA INSPIRED WEB APPLICATION FRAMEWORK FOR NODE

  • Built-in HTTP server
  • Request / response enhancements
  • View support
  • HTML & redirection helpers
  • Ability to create robust APIs quickly & easily
  • Model - View - Router architecture
  • Does not hide any Node functions

EXPRESS.JS

written by T.J. Holowaychuck

SAMPLE EXPRESS.JS CODE

var express = require('express'); 
var app = express();  
 
app.get('/', function(req, res) {
   res.render("index"); 
}); 
 
app.get('*', function(req, res) {
   res.send("Page not found"); 
}); 
 
app.listen(3000, function() { 
   console.log("Listening on port 3000");
});

USING MIDDLEWARE WITH EXPRESS

  • Intercept requests before it gets to any route
  • Useful for authentication, validation, data parsing, session management and more

EXPRESS VIEW ENGINES

  • Jade

  • EJS

  • Handlebars

  • Swig

  • Handlebars & Swig require a module

    • Consolidate.js,  Transformers

EXPRESS.JS EXAMPLE 

PROBLEMS WITH NODE & EXPRESS

  • Callback hell

doAsync1(function () {
  doAsync2(function () {
    doAsync3(function () {
      doAsync4(function () {
    })
  })
})

SOLVING CALLBACK ISSUES

  • async.js

  • Q

  • Promises (Node 0.12 supports ES6 promises)

  • Generators (Node 0.11 supports ES6 generators)

  • Koa.js — Express alternative with built-in generator support

  • Strongloop article

TESTING & DEBUGGING TOOLS

WHAT IS IO.JS?

  • Fork of Node.js

  • Not happy with Node.js governance

  • Dissatisfied with Node's release schedule & advancement

  • Successful in having Joyent hand off Node to an independent foundation

RESOURCES

slides url:  slides.com/markcalkins/nodejs-intro

provide feedback: https://joind.in/event/openwest-2015

Introduction to Node.js and Express.js

By Mark Calkins

Introduction to Node.js and Express.js

Node.js has captured a lot of attention in industry this last year. A number of companies are moving part or all of their server stack over to Node.js. This presentation will explore why companies are starting to do that. It will look at the architecture of Node.js and why its asynchronous, non-blocking, single threaded model works so well for many types of applications, especially real-time applications. The presentation will also explore why Express.js has become the most popular web framework to be used when building Node.js server applications.

  • 1,533