This document provides an overview of Node.js including its key features and common uses. It discusses Node.js' event-driven and non-blocking architecture using the V8 engine and libuv library. Common uses of Node.js include building APIs, rapid experimentation, application modernization, cross-platform experiences, and IoT applications. The document also covers callbacks, NPM, Express, and testing with Mocha.
5. 1. Build APIs
• BFF
2. Rapid experimentation
• Hackathons
3. Application modernization
• Strangler
4. Cross platform digital
experiences
• Electron
5. IoT applications
• Small footprint
Forrester Report 2016
Common use-cases
6. • Built almost twice as fast with fewer people
• Written in 33% fewer lines of code
• Constructed with 40% fewer files
PayPal case study
• Double the requests per second vs. the Java
application.
• 35% decrease in the average response time
for the same page.
Source: https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/
7. What is Node.js?
• Open source
• Cross platform
• Server-side
• Javascript runtime
• Event driven
• Non-blocking IO
• Scaleable
8. Runtime Architecture
• Google V8 => Open-source JavaScript engine,
JIT compiled, efficient GC, fast property access
• Libuv => Multi-platform, high performance,
event driven, async io library
• Event Loop => Single threaded, non-blocking,
executes all user code
• Bindings => Bind JavaScript to native
implementations
• Core Library => System JS libraries for File,
Network, Crypto and other basic functions
Source: https://nodejs.org/en/about/
9. Event-Loop
• Implemented in libuv
• Event-loop starts automatically at program
startup after initialization script is run
• Exits if there are no active callbacks
• Each phase has a FIFO queue of callbacks to
executed
• Network IO performed on single thread
• File IO performed using threads from pool
Event-loop
Source: http://docs.libuv.org/en/v1.x/design.html
11. 1. Most fundamental programming construct of Node.js
2. Introduces a different mental and programming paradigm
3. All async functions needs to have a callback defined
4. By convention callback is usually the last parameter
5. By convention, first parameter of callback is error
Callbacks in Node.js
Source: https://www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm
12. • NPM is package manager for JavaScript and Node.js
• World’s largest software registry (477,000)
• Install, share, distribute code and manage
dependencies
• Vibrant ecosystem
• Command line client
• OSS contribution
• 3,052,839,996 weekly package downloads
Node Package Manager (NPM)
Image source: Google search
13. • Minimal and flexible web application framework
• Robust set of features for web and mobile
applications
• Myriad of HTTP utility methods and middleware at
your disposal
• Creating a robust API is quick and easy
• De-facto app server framework for Node.js
• Integrates well with frontend frameworks like
JQuery, AngularJS, ReactJS
Express
Image source: Google search
14. • Feature-rich JavaScript test framework
• Simple and fun
• Test coverage reporting
• Async testing support
• Reports test durations
• before, after, before each, after each hooks
• Optionally run tests that match a regexp
• More…
TDD with Mocha
Image source: Google search
15. • GET /timeofday
Returns me current
timestamp
Demo – I need an API
• GET /timeofday
Authenticates request
• GET /timeofday?epoch=true
Returns me current time as
epoc value
Req1
Req2
Req3
Source: https://insights.stackoverflow.com/survey/2017
64,000 developers took our annual survey in January.
Developer profile – Geo, Type, Experience, Education, Gender, Ethnicity,
Source: https://stackify.com/trendiest-programming-languages-hottest-sought-programming-languages-2017/
Published November 2017
Here are the top 20 programming languages by total GitHub pushes
Lowe’s NASA Node.js Foundation NodeSource Solstice WillowTree YLD
BFF – Server side api to access SoR, downstream services
Source: https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/
5 java developers with 2 months headstart
2 Node developers
Meet at the same point
Source: http://docs.libuv.org/en/v1.x/design.html
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
timers: this phase executes callbacks scheduled by setTimeout() and setInterval().
I/O callbacks: executes almost all callbacks with the exception of close callbacks, the ones scheduled by timers, and setImmediate().
idle, prepare: only used internally.
poll: retrieve new I/O events; node will block here when appropriate.
check: setImmediate() callbacks are invoked here.
close callbacks: e.g. socket.on('close', ...).
Handles represent long-lived objects capable of performing certain operations while active.
Requests represent (typically) short-lived operations. These operations can be performed over a handle.