37
Ansuman Roy

Node js meetup

Embed Size (px)

Citation preview

Page 1: Node js meetup

Ansuman Roy

Page 2: Node js meetup

Agenda Introduction History Why Node.js Community Features FAQ Few Awesome

Modules! Conclusion

get ready to node, this session is hands on!

Page 3: Node js meetup
Page 4: Node js meetup

IntroductionIt’s NOT a web framework, and it’s also NOT a

languageOpen Source, cross-platform runtime

environment for server-side and networking applications

Based on Google V8 Engine Asynchronous i/o framework

Core in c++ on top of v8Rest of it in javascript Swiss army knife for network Related stuffsCan handle thousands of Concurrent connections

with Minimal overhead (cpu/memory) on a single process

Page 5: Node js meetup

Licence MIT Last release : v7.2.1385,151 + npm modules

Page 6: Node js meetup

History Initial Release May 27, 2009 Created by Ryan Dahl Development && maintenance sponsored by

Joyent

Page 7: Node js meetup

Is Node JS a serious option for enterprise applications?

In fact, it’s our best option for typical (heavy data IO) web applications!

Let’s see why…

Page 8: Node js meetup

Why Node?Less keystrokes with JSJS on server and client

allows for more code reuseA lite stack (quick create-

test cycle)1 Language for Frontend

and BackendLarge number of offerings

for web app creationActive community

Build Fast!

Page 9: Node js meetup

Why Node?Fast V8 EngineSingle Thread with

Event LoopGreat I/O

performance with event loop!

Small number of layers

Horizontal Scaling

Run Fast!

Page 10: Node js meetup

Why Node?JS across stack allows

easier refactoringSmaller codebaseCross

platform( Windows, Linux, Mac)

See #1 (Build Fast!)

Adapt Fast!

“There’s a shift in enterprise IT, unbundling the monolith software packages [at many companies] that has stifled innovation.” - Joe McCann CEO NodeSource

Page 11: Node js meetup

Community Over 8 billion downloads per month NPM averages 461 new modules per day

(ruby gems 37 per day, maven 156 per day) Total Number of npm Packages: 105,109 Second most stared project on Github Plus, corporate backing from Joyent

Page 12: Node js meetup

Community

Reference : http://www.modulecounts.com/

Page 13: Node js meetup

Community

Page 14: Node js meetup

Running Node Apps

Hands On #1 – ‘Welcome Noder!’

Page 15: Node js meetup

Node Globalsprocess – object providing information and

methods for the current process➢ process.stdout, process.stderr, process.stdin,

process.argv, process.env console – allows printing to stdout and

stderr require() – function to load a module module – refers to the current module

Page 16: Node js meetup

ModulesModules allow Node to be extended (act as

libaries)We can include a module with the global

require function, require(‘module’)Node provides core modules that can be

included by their name: File System – require(‘fs’) Http – require(‘http’) Utilities – require(‘util’)

Page 17: Node js meetup

ModulesWe can also break our application up into

modules and require them using a file path: ‘/’ (absolute path), ‘./’ and ‘../’ (relative to

calling file) Any valid type can be exported from a

module by assigning it to module.exports

Page 18: Node js meetup

Modulesbar.js

module.exports = ‘I am a string’

foo.js

var msg = require(‘./bar.js’)

console.log(msg) // prints ‘I am a string’

Page 19: Node js meetup

Modulesbar.js

module.exports.hello = function() {return ‘hello’}

module.exports.bye = function() {return ‘bye’}

foo.js

var bar = require(‘./bar.js’)

console.log(bar.hello()) // prints ‘hello’

console.log(bar.bye()) // prints ‘bye’

Page 20: Node js meetup

Requiring Modules, File System, Args

Hands On #2 – ‘Require It’

Page 21: Node js meetup

Node Architecture

V8Thread

Pool(libeio)

Event Loop

(libev)

Node Bindings (socket, http, etc.)

Node Standard Library

C

C++

JavaScript

Page 22: Node js meetup

Node ArchitectureNode handles requests with a single thread

(the event loop)!

Page 23: Node js meetup

Node ArchitectureHow can this be faster?

Expensive I/O is moved off request thread (in traditional multi-threaded environments, each thread handles the complete request)

Threads are limited by RAM and are expensive to create

Avoids context switching Allows us to easily write asynchronous code

without heavy thread management (synchronization, message passing, etc.)

Page 24: Node js meetup

Node Architecture

Page 25: Node js meetup

Node Architecture

Warning! Be careful to keep CPU intensive operations off the event loop.

Page 26: Node js meetup

Http and Streams

Hands On #3 – ‘Real Time Data Flow’

Page 27: Node js meetup

NPMModules can be shared by packagingNode Package Manager (NPM) is a package

manager for node Command line interface Public registry www.npmjs.org Allows us to install packages from repo, and

publish our own

Page 28: Node js meetup

NPMFirst, we need to create a package.json file

for our appContains metadata for our app and lists the

dependenciesPackage.json Interactive Guide

Page 29: Node js meetup

NPMCommon npm commands:

npm init initialize a package.json file

npm install <package name> -g install a package, if –g option is given package will be installed as a global, --save and --save-dev will add package to your dependencies

npm install install packages listed in package.json

npm ls –g listed local packages (without –g) or global packages (with –g)

npm update <package name> update a package

Page 30: Node js meetup

NPMSemVar Versions (version [Major].[Minor].

[Patch]): = (default), >, <, >=, <= * most recent version 1.2.3 – 2.3.4 version greater than 1.2.3 and less than 2.3.4 ~1.2.3 most recent patch version greater than or equal to 1.2.3

(>=1.2.3 <1.3.0) ^1.2.3 most recent minor version greater than or equal to 1.2.3

(>=1.2.3 <2.0.0)

Page 31: Node js meetup

Http and Express

Hands On #4 – ‘Services’

Page 32: Node js meetup

FAQ

V8 engine performs just as good, if not better than jvm Most ‘intensive’ code is usually due to I/O If needed, child processes, web workers, and so forth

can be used

○ Of course! As always, be aware of language holes (like ‘eval’)○ Node Security Project lists vulnerabilities in packages

What about CPU intensive code?

Is Node secure?

Page 33: Node js meetup

FAQ

○ Most ‘cryptic’ code is DOM related (ex. selectors)○ Preprocessors and ECMAScript 6 bringing more strict typing

(and compile time checking)○ As always, need standards and best practices!

What about code readability/maintenance with JavaScript?

Page 34: Node js meetup

FAQ

○ Built-in Node debugger not great, but IDEs and other tools have the usual debugging experience

○ V. 0.12 will improve tracing capabilities to allow better performance monitoring, and production grade tools such as New Relic

Isn’t debugging and production monitoring bad?

Page 35: Node js meetup

Other Awesome Modules skipper-gridfs – A skipper adapter to allow

uploading files to MongoDB's GridFS async – higher order functions and common

patterns for asynchronous code fs-extra– fs-extra contains methods that aren't

included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf

request – Simplified HTTP request client mongoose – MongoDB ODM

Page 36: Node js meetup

Other Awesome Modules sails– API-driven framework for building

realtime apps, using MVC conventions (based on Express and Socket.io)

express – Fast, unopinionated, minimalist web framework

jsreport– javascript based business reporting config – runtime configuration xls-to-json – Converting xls file to json files

using nodejs

Page 37: Node js meetup

Thanks!

http://nodejs.org/https://www.npmjs.com/

Learn More at