mini-training: node.js

24
NODE.JS HOW JAVASCRIT IS CHANGING SERVER PROGRAMMING MAXIME LEMAITRE – 16/12/2013

Upload: betclic-everest-group-tech-team

Post on 28-Jan-2015

119 views

Category:

Technology


0 download

DESCRIPTION

A quick overview on Node.js to see how Javascript is changing server programming : brief history & motivations, what makes NodeJS so special/popular/difficult, typical use cases, and a few popular tools around the NodeJs world : npm, Grunt, IISNode, Tools for Visual Studio, …

TRANSCRIPT

Page 1: Mini-Training: Node.js

NODE.JSHOW JAVASCRIT IS CHANGING SERVER PROGRAMMING

MAXIME LEMAITRE – 16/12/2013

Page 2: Mini-Training: Node.js

Agenda

• Introduction• What makes Node so ___ ?• How to Node ….– Hello world– Tweeter Console

• NPM to the rescue• … and not to Node– Grunt– Redis Commander

• Node.js and Microsoft• Questions

“Most languages were designed tosolve computational problems, butNode.js is different “

“ Node.js was designed from theground up to efficiently handle thecommunication that is at the heartof modern web applications “

Page 3: Mini-Training: Node.js

Quick overview

• Brief History– Invented by Ryan Dahl of Joyent, spring 2009 (4 years)

• Solving the upload progress bar problem on Flickr– Jan. 2012 Dahl stepped aside, promoting coworker and NPM creator Isaac Schlueter– First Stable Build on Windows : v0.6.0 (November 2011)

• With the Help of Microsoft

Node's goal is to provide an easy way to build scalable network programs

• Now– Development and maintenance is sponsored by Joyent (Cloud infrastructure)

• But many contributors : StrongLoop, Voxer, Joyent, Microsoft, Mozilla, …– #3 repository on GitHub (500 contributors, 10 000 commits)– V0.10 (March 2013) : 35 000 downloads /day, 1 000 000 downloads in 3 months– Run on Linux, Mac, Windows, … and in the Cloud (Supported by major Cloud Platforms)

Page 4: Mini-Training: Node.js

“I/O needs to be done differently”Inspiration

• Many web applications have code, like this:

What is the software doing while it queries the database?In many cases, just waiting for the response.

(blocks the thread)• But a line of code like this

allows the program to return to the event loop immediately.This is how I/O should be done.

Page 5: Mini-Training: Node.js

Typical I/O latency

Page 6: Mini-Training: Node.js

What makes Node so special ?

• Server-side JavaScript– most widely used programing language of the web– async by nature

• Non-blocking I/O– do not wait slow ressources to respond

• Built using V8 JavaScript Engine (from Chrome)– very fast and getting faster after each release

• « Event-based » or « Asynchronous »– all execution initiated by an event– « Everything runs in parallel except your code »

• Single-threaded Event Loop, limited to one CPU– no threads– no locks or no execution concurrency– events are executed in order

Page 7: Mini-Training: Node.js

Node.js Processing Model

Page 8: Mini-Training: Node.js

What makes Node.js so popular ?

• It’s fast and scalable• JavaScript all the way

– Minimal learning curve– JS is now the ubiquitous language of the web– Allow code re-use between front-end and back-end.

• Web centric– Being based on JS and V8, node.js naturally attracts mainly web developers

• Minimalist Core API and efficient module system • Active development• Always asynchronous

– All I/O done in Node is by design asynchronous• Non-fragmented community

Page 9: Mini-Training: Node.js

What makes Node.js so difficult ?

Asynchronous code Single Event Loop

Easy to write code like this(async may help you)

if # entries = 10,000doSomething() takes ~1msyou block for 10 seconds!

each node process is bound to one coreeverything you do blocks

Page 10: Mini-Training: Node.js

Companies using node

https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node

Page 11: Mini-Training: Node.js

• Good Use Case– JSON API– Streaming– Real-time Pub/Sub systems– Chatty apps– Dashboards– Queues– Proxy

• Bad Use Case– CPU heavy apps– Memory intensive apps– Simple CRUD / HTML apps

Finally, is it good for everything ?

Page 12: Mini-Training: Node.js

12

Node.js at PaypalNov 2013, https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/

• Node.js version Vs Java– Built almost twice as fast with fewer people– Written in 33% fewer lines of code– Constructed with 40% fewer files

• Node.js performance Vs Java– Double the requests per second vs. the Java application– 35% decrease in the average response time

Page 13: Mini-Training: Node.js

Hello World

Page 14: Mini-Training: Node.js

Tweeter Console

Page 15: Mini-Training: Node.js

NPM : Node package managerinnovation through modularity

• Set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management – Preinstalled with Node.js– Approx. 50 544 online packages (Dec 2013)– +150 packages per day (#1 of all packages managers)

• Features– publish, install, discover, and develop node programs– open to all, and anyone can publish their own module– puts modules in a place where node.js can find them– Manage dependencies & modules with package.json

“Node.js is successful because of npm”

“don’t worry about multiple versions causing conflicts because npm will automatically partition them. In other words: it just works.”

Page 16: Mini-Training: Node.js

Interesting Modules

Express Web application framework for nodeUnderscore JavaScript's utility belt _Commander Complete solution for node.js command-line interfacesAsync Higher-order functions and common patterns for asynchronous codeSocket.IO Realtime framework, with WebSockets and cross-browser fallbacks supportnode_redis redis client for nodeJade Server-side templating engineRequest Standard HTTP clientnode-unit Easy unit testing for node.js and the browserwinston a multi-transport async logging library for node.jsForever simple CLI tool for ensuring that a given script runs continuouslynode-sqlserver Microsoft Driver for Node.js for SQL Servergoogle-api Official client library for accessing Google APIsazure-sdk Azure SDK for Node.js

… also more than 50 000 packages

Page 17: Mini-Training: Node.js

Grunt : The Javascript Task RunnerThe Build Tool for JavaScript and Web Applications

“Grunt is a task-based command line build tool for JavaScript projects.”Þ Use the Power of JavaScript/Node to Automating Repetitive Tasks

• Common Build Tasks (configured in gruntfile.js)– Concatenate grunt-contrib-concat– Compress grunt-contrib-compress– Minify grunt-contrib-htmlmin, grunt-contrib-cssmin– Linting (JsLint, JsHint) grunt-contrib-jshint , grunt-contrib-csslint– Testing grunt-contrib-qunit, grunt-contrib-nodeunit– Optimize Image grunt-contrib-imagemin – Monitor Files grunt-contrib-watch– Publish grunt-contrib-copy– YOUR TASK HERE

• Who uses Grunt ?– Twitter, Jquery, Bootstrap, WordPress, Adobe, Pinterest, Facebook, …

Page 18: Mini-Training: Node.js

Redis CommanderBuilt with Node

Page 19: Mini-Training: Node.js

IISNodeHost Node.js app in IIS

• Native IIS module that allows hosting of node.js applications in IIS on Windows. – Node Process management– Side by side with other content types.– Scalability on multi-core servers– Integrated debugging– Auto-update– Access to logs over HTTP– Minimal changes to node.js application

code– Integrated management experience– Other IIS benefits : Port sharing,

security, URL rewriting, compression, caching, logging

Important Node : Windows Azure Node.js hosting is powered by IISNode !

Page 20: Mini-Training: Node.js

Node.js for Visual Studio UsersNode.js Tools for Visual Studio (Alpha)

Turns Visual Studio into a Node.js IDE.• Editing• Intellisense• Profiling• Npm integration• Debugging locally and

remotely (Windows/Mac/Linux)

• Azure Deployment• …

You can also try

Page 21: Mini-Training: Node.js

Questions

Page 22: Mini-Training: Node.js

References

• Really good introduction– http://www.nodebeginner.org/– http://nodejs.developpez.com/tutoriels/javascript/node-js-livre-debutant/ - FRENCH-

• http://nodejs.org/ (Official)• http://nodejs.org/api/ (Official Api)• http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js• https://github.com/maxogden/art-of-node#the-art-of-node• http://tomasz.janczuk.org/2011/08/hosting-nodejs-applications-in-iis-on.html• http://package.json.nodejitsu.com/• https://blog.nodejitsu.com/npm-innovation-through-modularity• https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/• http://www.youtube.com/watch?v=SAc0vQCC6UQ• http://cgbystrom.com/articles/six-reasons-why-node-js-is-so-popular/• http://www.ebaytechblog.com/2013/05/17/how-we-built-ebays-first-node-js-appli

cation/

Page 23: Mini-Training: Node.js

Find out more

• On https://techblog.betclicgroup.com/

Page 24: Mini-Training: Node.js

About Betclic• Betclic Everest Group, one of the world leaders in online gaming, has a unique portfolio

comprising various complementary international brands: Betclic, Everest Gaming, bet-at-home.com, Expekt…

• Active in 100 countries with more than 12 million customers worldwide, the Group is committed to promoting secure and responsible gaming and is a member of several international professional associations including the EGBA (European Gaming and Betting Association) and the ESSA (European Sports Security Association).

• Through our brands, Betclic Everest Group places expertise, technological know-how and security at the heart of our strategy to deliver an on-line gaming offer attuned to the passion of our players.