Intro to Node.js (v1)

30
Intro to Node.js Chris Cowan Lead Engineer http://www.plus3network.com

description

This is a presentation I prepared for a local meetup. The audience is a mix of web designers and developers who have a wide range of development experience.

Transcript of Intro to Node.js (v1)

Page 1: Intro to Node.js (v1)

Intro to Node.jsChris Cowan

Lead Engineerhttp://www.plus3network.com

Page 2: Intro to Node.js (v1)

What is Node.js?• Create by Ryan Dahl• Uses Chrome’s V8 Engine• Uses Non-Blocking I/O• Single Threaded• Server Side JavaScript• Active Community

Page 3: Intro to Node.js (v1)

Node’s Goal is to provide an easy way to build scalable

network programs.

Page 4: Intro to Node.js (v1)

Node.js is NOT anotherweb framework!

But you can create a web framework using NPM modules.

Page 5: Intro to Node.js (v1)

Node.js is…Web ServerTCP Server

Awesome Robot ControllerCommand Line Application

Proxy ServerStreaming ServerVoiceMail Server

Anything that has to deal with high I/O

Page 6: Intro to Node.js (v1)

Node.js isServer Side JavaScript!

Page 7: Intro to Node.js (v1)

Why Node.js?• Non Blocking I/O• Based on Chrome’s V8 Engines (FAST!)• 15,000+ Modules • Active Community (IRC, Mailing Lists, Twitter,

Github)• Mac, Linux and Windows (all first class citizens)• One Language for Frontend and Backend• JavaScript is the Language of the Web

Page 8: Intro to Node.js (v1)

Installing Node.jsMac OS X1. Go to http://nodejs.org and click install2. Install the downloaded package

Windows3. Go to http://nodejs.org and click install4. Install the downloaded package

Linux (and *nix variants)5. Go to http://nodejs.org and click install6. Decompress source and… ./configure … make … make install

( for Ubuntu use Chris Lea’s PPA – ppa:chris-lea/node.js )

Page 9: Intro to Node.js (v1)

Some BasicExamples

Page 10: Intro to Node.js (v1)

Hello WorldCreate hello-world.js

console.log(‘Hello World’);

On the command line run

node hello-world.js

You should see

Hello World

Page 11: Intro to Node.js (v1)

Basic HTTP Server

*Running this script my development box, I can achieve 10,000+ requests per second

with 100 concurrent connectionswithout breaking a sweat

Page 12: Intro to Node.js (v1)

Some people use the core http module to

build their web apps, most use a framework

like Expressor Connect or Flatiron or Tako or Derby or Geddy or Mojito or …

Page 13: Intro to Node.js (v1)

Visithttp://expressjs.com/guide.html

for a detailed guide on using Express

Page 14: Intro to Node.js (v1)

What is Non-Blocking I/O?And why should I care?

Page 15: Intro to Node.js (v1)

Blocking I/

270ms = SUM(user, activities, leaderboard)

Page 16: Intro to Node.js (v1)

Non-Blocking I/

150ms = MAX(user, activities, leaderboard)

Page 17: Intro to Node.js (v1)

The most jarring thing about Server Side JavaScript

is thinking in callbacks

Page 18: Intro to Node.js (v1)

Callbacks are the Devil’s Work!Don’t go down this rabbit hole…

One of the biggest mistakes is to get yourself in to callback hell by nesting callbacks inside of

callbacks inside of more callbacks.

Page 19: Intro to Node.js (v1)

Avoiding Callback Hell• Keep your code shallow• Break up your code into small chunks• Use a sequential library like async• Visit http://callbackhell.com

Page 20: Intro to Node.js (v1)

Async to the rescue!

Page 21: Intro to Node.js (v1)

Visithttps://github.com/caolan/async

for a detailed guide on using the async module.

Async provides several useful patterns for asynchronous control flow

including: parallel, series, waterfall, auto and queue.

Page 22: Intro to Node.js (v1)

The Node Package Managerotherwise know as… NPM

It’s how you harness the awesomeness of the Node.js community!

Page 23: Intro to Node.js (v1)

Using NPMIt’s standard practice to install modules locally for your current project. Modules are installed in the ./node_modules in the current directory.

To Install a new module

npm install <module>

To find a module in the NPM repository

npm search <search string>

To list the modules (and their dependencies) in the current project

npm list

To see module details

npm info <module>

Page 24: Intro to Node.js (v1)

DON’T INSTALL MODULES GLOBALLY!

Unless they are tools like node-dev, jake, express, minify-js OR linked development modules but more on that later.

Page 25: Intro to Node.js (v1)

NPM is awesome sauce!

Visithttps://npmjs.org

for more details about NPM and to browse the current NPM Repository

Page 26: Intro to Node.js (v1)

Creating your own modules• Node.js uses CommonJS Modules• require(‘./example’) will load either

example.js or example/index.js or the entry point specified in package.json

• Run npm init to bootstrap your new module• Try to stick to creating Pure JavaScript

modules if possible. It will give you less headaches down the road.

Page 27: Intro to Node.js (v1)

Basic Module ExampleEverything exposed via module.exports is available as an instance variable.

Once you’ve created a module you use it like this…

Keep this in mind… modules are loaded once and cached. So when you load the module a second time in your app, require just returns the cache copied. This lets you do interesting things…

Page 28: Intro to Node.js (v1)

Installing your module• Run npm link in the module working directory• Then run npm link <module> in the your

project folder to link it from the global module to your local node_modules.

• OR you can create a private registry (See https://npmjs.org/doc/registry.html)

• OR just link it by hand :P

Page 29: Intro to Node.js (v1)

My Favorite Modules• request• async• node-dev• underscore• express

• jake• hogan.js• connect• moment• mysql

Page 30: Intro to Node.js (v1)

Questions?Contact me at [email protected]