Top Banner
A MEGA-FAST INTRODUCTION TO Kasey McCurdy Director of Engineering @ Bunchball
36

An introduction to Node.js

Jul 14, 2015

Download

Internet

Kasey McCurdy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: An introduction to Node.js

A MEGA-FAST INTRODUCTION TO

Kasey McCurdy Director of Engineering @ Bunchball

Page 2: An introduction to Node.js

A 10-MINUTE TOUR OF JAVASCRIPT

Page 3: An introduction to Node.js

JAVASCRIPT THEN…

• Developed in 1995 Netscape to script it’s Navigator Browser

• Has zero* to do with Java

• Used in the early days of the web to annoy the shit out of people (and do things like form validation, image rollovers, and basic interactivity)

Page 4: An introduction to Node.js

JAVASCRIPT NOW…

• Huge surge in popularity since 2005 thanks to countless innovations…

• Standardization (ECMA)

• Javascript libraries such as jQuery

• XHR / AJAX

• Better browsers powered by faster and faster Javascript engines

Page 5: An introduction to Node.js

JAVASCRIPT IS NOW QUITE POPULAR…

Page 6: An introduction to Node.js

SO, WHAT MAKES JAVASCRIPT UNIQUE?

Loosely Typed

Object-based

Functions are First-Class

Functional Scoping

Can be asynchronous (demo)

Page 7: An introduction to Node.js

ASYNCHRONOUS DEMO

Page 8: An introduction to Node.js

VROOM.

Page 9: An introduction to Node.js

THE V8 JAVASCRIPT ENGINE

• Developed by Google for the Chrome browser in 2008

• Written in C++

• Compiles Javascript to machine code before execution vs. interpreting the code

• Blew away the competition.

( 2008 Tests )

Page 10: An introduction to Node.js

BECAUSE OF V8, WE HAVE NODE.JS

• Invented in 2009 by Ryan Dahl @ Joyent

• Uses the V8 Javascript engine at its core

• Node.js runtime contains core modules that handle everything from HTTP, Filesystem I/O, Cryptography, etc.

• Browser-specific items taken out (Document, Window, etc)

• This allows us to run Javascript anywhere…the command line…the server…hardware…anywhere…

• Writing Javascript everywhere is awesome.

Page 11: An introduction to Node.js
Page 12: An introduction to Node.js

AN OVERVIEW OF NODE.JS

• Create anything from command line utilities to lightweight API’s to full-scale web applications.

• 3rd most popular project on GitHub

• Great for high-traffic applications,

• Not-so-great for high-CPU applications.

• Highly-active community

• Used (and endorsed) by many large corporations - Walmart, eBay, Apple, etc…

Page 13: An introduction to Node.js

NODE.JS SUCCESS STORIES

LinkedIn switched to Node.js from Rails for their mobile traffic, reducing the number of servers from 30 to 3 (90% reduction) and the new system was up to 20x faster.

PayPal are rolling out Node.js across their entire web application and Node.js will be used almost everywhere within 2 years.

On Black Friday, the Walmart servers didn’t go over 1% CPU utilization and the team did a deploy in the middle of the day with 200,000,000 users online.

For more companies and examples of Node.js in the wild: http://bit.ly/node-usage

Page 14: An introduction to Node.js

NODE IS EVENT-DRIVEN

• Node runs on a single-threaded, non-blocking event loop.

• The event loop essentially contains a queue of callback functions.

• Once expensive operations like Disk I/O or DB connections are finished, the callback function is executed.

• Radically different from blocking languages, like PHP.

Page 15: An introduction to Node.js

SYNCHRONOUS ASYNCHRONOUS

BLOCKING NON-BLOCKING

Page 16: An introduction to Node.js
Page 17: An introduction to Node.js

NODE PACKAGE MANAGER

• Over 135,000 modules, extending the functionality of Node

• Everything from small utilities to full-fledged frameworks

• Comes bundled automatically with Node

• Provides for dependency management

• Easy to install a module: npm install moduleName

• Modules can be installed globally (with the “-g” flag) or embedded within your project in the “node_modules” directory

• Modules can be used in a Node program using require(‘moduleName’)

Page 18: An introduction to Node.js

PACKAGE.JSON

• Holds various metadata relevant to the project (name, version, etc.)

• Used primarily for dependency-management

• Initialize an empty package.json file with “npm init”

• Install dependencies for a project by running “npm install” from the project root

• You can save dependencies to your project automatically to package.json by typing “npm install <module> --save”

Page 19: An introduction to Node.js

EVERYONE’S FAVORITE MODULE…

Page 20: An introduction to Node.js

• Web application framework, designed for building single-page, multi-page, and hybrid web applications

• Built on top of another framework called Connect

• Similar to Sinatra (a ruby framework), provides MVC capabilities

• Minimalist, yet full-featured

• Built-in support for routing & various HTTP handlers, configuration, session management, and middleware

• Amazing community

MY FAVORITE MODULE…EXPRESS.JS

Page 21: An introduction to Node.js

EXPRESS.JS : ROUTING

Page 22: An introduction to Node.js

EXPRESS.JS : ROUTING

Page 23: An introduction to Node.js

EXPRESS.JS : MIDDLEWARE

Middleware Overview

Page 24: An introduction to Node.js

EXPRESS.JS : MIDDLEWARE• Middleware is a pipeline of code that gets called before your

request handler

• Express applications are basically a bunch of middleware calls

• Middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application, commonly denoted by a variable named next

• Middleware can:

• Execute any code.

• Make changes to the request and the response objects.

• End the request-response cycle.

• Call the next middleware in the stack.

Page 25: An introduction to Node.js

EXPRESS.JS : MIDDLEWARE

Middleware Example

Page 26: An introduction to Node.js

EXPRESS.JS : TEMPLATING

Templating Example

Page 27: An introduction to Node.js

EXPRESS.JS : TEMPLATING

Page 28: An introduction to Node.js

RANDOM THINGS TO BE AWARE OF…

• The pyramid of doom aka “callback hell” — http://callbackhell.com (Async.js is one module that can help)

• Writing asynchronous code can be hard…you have to think differently.

• Node.js vs. IO.js

• Node process can be kept alive with modules like “Forever”

• Play around with free & easy hosting of Node apps on Heroku

Page 29: An introduction to Node.js

BONUS ROUND

Page 30: An introduction to Node.js

MONGODB : WE DON’T NEED NO JOINS

• Document-oriented, NoSQL database

• Goodbye table-based relational database structures…Hello JSON-like documents with dynamic schemas

• Highly performant when MongoDB’s awesome indexing support is used

• Highly available, with failover and replication built-in

• A natural fit for Node and Express applications

• Uses JSON for queries

• Did I mention no joins?

Page 31: An introduction to Node.js

MONGODB : DOCUMENT-BASED

A sample document in mongoDB…notice the schema differences.

Page 32: An introduction to Node.js

MONGODB : QUERY WITH JSON!

SQL Query

mongoDB Query

SELECT * FROM users WHERE last_name="gullion"

db.users.find({ "last_name": "gullion"});

Page 33: An introduction to Node.js

MONGODB : QUERY WITH JSON!SQL Query

mongoDB Query

SELECT * FROM students WHERE GPA > 2.5 AND major = "comp sci"

db.students.find({ "GPA": { "$gt": 2.5 }, "major": "comp sci"});

Page 34: An introduction to Node.js

• Command-line utilities

• Real-time applications

• Apps with many concurrent users

• APIs

Good For:

• CPU-intensive apps

• Simple HTML websites

Not So Good For:

Node is not a framework, it is a platform.

IN SUMMARY…

Page 35: An introduction to Node.js

• http://nodeschool.io/#workshoppers

• https://www.codeschool.com/courses/real-time-web-with-node-js

• http://www.slideshare.net/crashposition/fast-slim-correct-the-evolution-of-javascript

• https://medium.com/unexpected-token/10-weeks-of-node-js-after-10-years-of-php-a352042c0c11

• https://devcenter.heroku.com/articles/getting-started-with-nodejs

FURTHER READING / RESOURCES…

• http://shop.oreilly.com/product/0636920032977.do

• http://www.manning.com/cantelon/

Books

Websites / Blogs

Page 36: An introduction to Node.js

OK, LET’S CODE SOME NODE…

http://bit.ly/dmaccnode