33
NodeJS - © Claudio Cicali What the frak is Node JS? Tuesday, December 4, 2012

Introduction to NodeJS

  • View
    25

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to NodeJS

NodeJS - © Claudio Cicali

What the frak is Node JS?

Tuesday, December 4, 2012

Page 2: Introduction to NodeJS

NodeJS - © Claudio Cicali

Claudio Cicali

@caludio

Front-end engineer at SponsorPay

Tuesday, December 4, 2012

Page 3: Introduction to NodeJS

NodeJS - © Claudio Cicali

Installing NodeJS

Download, unpack, make, make install

... or use homebrew

(and yes! It works on Windows too)

Tuesday, December 4, 2012

Page 4: Introduction to NodeJS

NodeJS - © Claudio Cicali

JavaScript is an interpreted languagewhich means that you need an JavaScript

interpreter to run your programs

We often refer to these beastsas JavaScript Engines or

JavaScript Virtual Machine

Tuesday, December 4, 2012

Page 5: Introduction to NodeJS

NodeJS - © Claudio Cicali

Every browser has its own VM

Firefox? SpidermonkeyInternet Explorer? Chakra

Chrome? V8Safari? JavaScriptCore

Opera? Carakan

Also Rhino, stand alone

Tuesday, December 4, 2012

Page 6: Introduction to NodeJS

NodeJS - © Claudio Cicali

2009: this guy had an idea

(he is Ryan Dahl, btw)

Tuesday, December 4, 2012

Page 7: Introduction to NodeJS

NodeJS - © Claudio Cicali

Make no mistake

NodeJS is NOT a “one man project”

It is backed by Joyent, which is a big playerin the cloud computing business

(has money and people)

Tuesday, December 4, 2012

Page 8: Introduction to NodeJS

NodeJS - © Claudio Cicali

If you already can write JavaScriptin the browser why can’t you writeJavaScript on the server then?

Tuesday, December 4, 2012

Page 9: Introduction to NodeJS

NodeJS - © Claudio Cicali

js VMs are getting better and better

Get one of them, include someadditional libraries and wrap it up

into a powerful, directly executableJavaScript platform

Tuesday, December 4, 2012

Page 10: Introduction to NodeJS

NodeJS - © Claudio Cicali

After the idea, the design:

Google V8 would have been the VM(Tried using Spidermonkey, no luck)

Monoprocess, no threads

Completely event driven (asynchronous, non-blocking)

Focus on NETWORKING

Tuesday, December 4, 2012

Page 11: Introduction to NodeJS

NodeJS - © Claudio Cicali

What is the “Node Standard Library”?

A set of ready-to-use tools which gives you

- a powerful HTTP(S) library (client and server in no time)- DNS name resolution- Crypto- access to the file system- URL manipulation- ZLIB- UDP datagrams

http://nodejs.org/docs/latest/api/

Tuesday, December 4, 2012

Page 12: Introduction to NodeJS

NodeJS - © Claudio Cicali

Those tools can JavaScript onlyor mixed with some C or C++

for performances reason

Tuesday, December 4, 2012

Page 13: Introduction to NodeJS

NodeJS - © Claudio Cicali

Now we have a platform to write REAL programs(i.e.: no sandboxing) in JavaScript

The JavaScript itself - oh, joy - it’s the latest stable V8

AND NO COMPATIBILITY LAYER! (cries)

except ...

Tuesday, December 4, 2012

Page 14: Introduction to NodeJS

NodeJS - © Claudio Cicali

... except for Node JS itself

With every new major version, you mightloose compatibility at the API level

(the Node standard library)

(this is really more of a youth problem, anyway)

Tuesday, December 4, 2012

Page 15: Introduction to NodeJS

NodeJS - © Claudio Cicali

Node JS is fast, in theory

Low maintencance (small impact on resources)

Programs must not stress the CPU

Programming is not THAT easy

API are well documented

Tuesday, December 4, 2012

Page 16: Introduction to NodeJS

NodeJS - © Claudio Cicali

The Node JS standard library can be EXTENDED by the use of

modules

This is serious business: the number of available modules is

HUGE

(and the quality? Well...)

Tuesday, December 4, 2012

Page 17: Introduction to NodeJS

NodeJS - © Claudio Cicali

MODULES in NodeJS use a well knownpattern/paradigm/standard: CommonJS(which is well known thanks to... NodeJS)

Writing a module is easy

Maybe too much :)

(oh, and you’d write “addons” in C/C++ as well)

Tuesday, December 4, 2012

Page 18: Introduction to NodeJS

NodeJS - © Claudio Cicali

Take a look:

https://github.com/joyent/node/wiki/modules

Tuesday, December 4, 2012

Page 19: Introduction to NodeJS

NodeJS - © Claudio Cicali

Module management is EASY with

https://npmjs.org/

No need to install it: comes with Node!(Node >= 0.6)

With brew you need to run npmjs.org/install.sh

Tuesday, December 4, 2012

Page 20: Introduction to NodeJS

NodeJS - © Claudio Cicali

Need to access MySQL from your program?

npm install mysql

Tuesday, December 4, 2012

Page 21: Introduction to NodeJS

NodeJS - © Claudio Cicali

Publishing your module to the worldis freacking easy too

(no moderation, no approval...)

npm publish

Tuesday, December 4, 2012

Page 22: Introduction to NodeJS

NodeJS - © Claudio Cicali

Modules can be of any complexity

Connect is a big “middleware” modulewhich gives you cookies, sessions, logging,

caching...

Express is a web framework built on top of Connect

Tuesday, December 4, 2012

Page 23: Introduction to NodeJS

NodeJS - © Claudio Cicali

//  The  “fs”  module  is  ready  to  usevar  fs  =  require(“fs”);

fs.open(“somefile”,  “r”,  function(error)  {

//  Do  something  with  the  file

});

What “using modules” means

Tuesday, December 4, 2012

Page 24: Introduction to NodeJS

NodeJS - © Claudio Cicali

claudioc$  npm  install  sqlite

//  Once  installed  the  “sqlite”  module  is//  ready  to  usevar  sqlite  =  require(“sqlite”);

var  db  =  new  sqlite.Database();

db.open(“my_db”,  function(error)  {

//  Do  something  with  the  database

});

Tuesday, December 4, 2012

Page 25: Introduction to NodeJS

NodeJS - © Claudio Cicali

There are libraries which run in the browserAND in NodeJS as modules (transparently)

“As of 3.5.0, YUI runs natively on Node.js and comes with an official npm package for easy installation.”

Tuesday, December 4, 2012

Page 26: Introduction to NodeJS

NodeJS - © Claudio Cicali

What can NodeJS be used for then?

Network servers (HTTP, Proxies, messaging)

API backends

Real time applications

Streaming data

“One page” web sites

Command line tools

BotsTuesday, December 4, 2012

Page 27: Introduction to NodeJS

NodeJS - © Claudio Cicali

Tuesday, December 4, 2012

Page 28: Introduction to NodeJS

NodeJS - © Claudio Cicali

Let me introduce you a coupleof things I’ve done with NodeJS

Tuesday, December 4, 2012

Page 29: Introduction to NodeJS

NodeJS - © Claudio Cicali

antipad.comReal time collaborative code editor

Built on the giants’ shoulders:

The ACE editor (awesome)

The ShareJS library (super awesome)

NodeJS (lovely)

Tuesday, December 4, 2012

Page 30: Introduction to NodeJS

NodeJS - © Claudio Cicali

jecho

“Poor man” command line remote debuggerfor mobile devices

https://github.com/claudioc/jecho

Tuesday, December 4, 2012

Page 31: Introduction to NodeJS

NodeJS - © Claudio Cicali

I want moar!

http://www.nodebeginner.org/(highly recommended)

http://nodetoolbox.com

http://nodeguide.com

Tuesday, December 4, 2012

Page 32: Introduction to NodeJS

NodeJS - © Claudio Cicali

Follow me! @caludioTuesday, December 4, 2012

Page 33: Introduction to NodeJS

NodeJS - © Claudio Cicali

One more thing

“The guys that are getting paid the big bucks to deliver scalable solutions aren’t up at night feverishly rewriting their systems in Node. They’re doing what they’ve always done: measuring, testing, benchmarking, thinking hard, keeping up with the academic literature that pertains to their problems.”

-- Alex Payne

http://al3x.net/2010/07/27/node.html

Tuesday, December 4, 2012