This document provides a high-level overview of JavaScript and Node.js. It discusses how JavaScript originated as a browser scripting language but is now widely used for server-side applications through Node.js. Node.js uses Google's V8 JavaScript engine and allows JavaScript code to run outside of browsers. Popular Node.js frameworks like Express.js are introduced. Key features of JavaScript like asynchronous programming and Node.js concepts like modules, middleware, and MongoDB are summarized at a high level.
3. 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)
4. 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
9. 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 )
10. 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.
11.
12. 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…
13. 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
14. 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.
17. 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’)
18. 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”
20. • 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
24. 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.
28. 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
30. 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?
32. MONGODB : QUERY WITH JSON!
SQL Query
mongoDB Query
SELECT * FROM users WHERE last_name="gullion"
db.users.find({
"last_name": "gullion"
});
33. 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"
});
34. • 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…