an introduction to node.js
TRANSCRIPT
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)
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
SO, WHAT MAKES JAVASCRIPT UNIQUE?
Loosely Typed
Object-based
Functions are First-Class
Functional Scoping
Can be asynchronous (demo)
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 )
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.
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…
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
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.
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’)
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”
• 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
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.
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
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?
MONGODB : QUERY WITH JSON!
SQL Query
mongoDB Query
SELECT * FROM users WHERE last_name="gullion"
db.users.find({ "last_name": "gullion"});
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"});
• 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…
• 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