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
SHOPPING LIST
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
SOCKET.IO
SOCKET.IO SAMPLE APP
CHAT APP
code: https://github.com/rauchg/chat-example/blob/master/index.js
SINATRA INSPIRED WEB APPLICATION FRAMEWORK FOR NODE
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
EXPRESS VIEW ENGINES
Jade
EJS
Handlebars
Swig
Handlebars & Swig require a module
Consolidate.js, Transformers
EXPRESS.JS EXAMPLE
SIMPLE CMS APP
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
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