• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Intro to Node.js (v1)
 

Intro to Node.js (v1)

on

  • 1,676 views

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.

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.

Statistics

Views

Total Views
1,676
Views on SlideShare
1,675
Embed Views
1

Actions

Likes
0
Downloads
44
Comments
0

1 Embed 1

https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Intro to Node.js (v1) Intro to Node.js (v1) Presentation Transcript

    • Intro to Node.js Chris Cowan Lead Engineer http://www.plus3network.com
    • 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
    • Node’s Goal is to provide an easy way to build scalable network programs.
    • Node.js is NOT another web framework! But you can create a web framework using NPM modules.
    • Node.js is… Web Server TCP Server Awesome Robot Controller Command Line Application Proxy Server Streaming Server VoiceMail ServerAnything that has to deal with high I/O
    • Node.js isServer Side JavaScript!
    • 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
    • Installing Node.jsMac OS X1. Go to http://nodejs.org and click install2. Install the downloaded packageWindows1. Go to http://nodejs.org and click install2. Install the downloaded packageLinux (and *nix variants)1. Go to http://nodejs.org and click install2. Decompress source and… ./configure … make … make install ( for Ubuntu use Chris Lea’s PPA – ppa:chris-lea/node.js )
    • Some Basic Examples
    • Hello WorldCreate hello-world.jsconsole.log(‘Hello World’);On the command line runnode hello-world.jsYou should seeHello World
    • Basic HTTP Server *Running this script my development box, I can achieve 10,000+ requests per second with 100 concurrent connections without breaking a sweat
    • Some people use the core http module tobuild their web apps,most use a framework like Express or Connect or Flatiron or Tako or Derby or Geddy or Mojito or …
    • Visithttp://expressjs.com/guide.html for a detailed guide on using Express
    • What is Non-Blocking I/O? And why should I care?
    • Blocking I/270ms = SUM(user, activities, leaderboard)
    • Non-Blocking I/150ms = MAX(user, activities, leaderboard)
    • The most jarring thingabout Server Side JavaScript is thinking in callbacks
    • 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.
    • Avoiding Callback Hell• Keep your code shallow• Break up your code into small chunks• Use a sequential library like async• Visit http://callbackhell.com
    • Async to the rescue!
    • Async provides several useful patterns for asynchronous control flow including: parallel, series, waterfall, auto and queue. Visit https://github.com/caolan/asyncfor a detailed guide on using the async module.
    • The Node Package Manager otherwise know as… NPM It’s how you harness the awesomeness of the Node.js community!
    • 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 modulenpm install <module>To find a module in the NPM repositorynpm search <search string>To list the modules (and their dependencies) in the current projectnpm listTo see module detailsnpm info <module>
    • DON’T INSTALLMODULES GLOBALLY! Unless they are tools like node-dev, jake, express, minify-js OR linked development modules but more on that later.
    • NPM is awesome sauce! Visit https://npmjs.orgfor more details about NPM and tobrowse the current NPM Repository
    • 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.
    • 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 whenyou load the module a second time in your app, require justreturns the cache copied. This lets you do interesting things…
    • 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
    • My Favorite Modules• request • jake• async • hogan.js• node-dev • connect• underscore • moment• express • mysql
    • Questions?Contact me at chris@chriscowan.us