Agenda
- What is Node.js
- Architecture
- Understanding Concepts
- Programming & Demos
What is Node.js
The Node.js website defines it as:
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
What is Node.js
I would define it as:
Node.js is an event-driven, non-blocking I/O, cross-platform runtime environment for building server side and networking applications using JavaScript
A brief History of Node.js
- Created by Ryan Dahl & published for Linux in 2009
- Later sponsored by Joyent
- Package Manager (NPM) was introduced in 2011
- Native Windows implementation released in June 2011
- io.js a fork of Node.js appears in December 2014
- Node.js Foundation was announced in February 2015
- io.js & Node.js first merged release September 2015
- First LTS (4.2.0) released in October 2015
- Latest release (5.1.0) released in November 2015
V8
V8 is a JavaScript engine written in C++.
- JIT (Just-In-Time) compiler instead of a JavaScript interpreter.
- V8 implements ECMAScript as specified in ECMA-262
- It can run standalone, or can be embedded into any C++ application.
- Implements an efficient Garbage Collection mechanism to reclaim memory
Libuv
libuv is a multi-platform support library with a focus on asynchronous I/O.
- Full-featured event loop backed by epoll, kqueue, IOCP, event ports.
- Asynchronous TCP and UDP sockets
- Asynchronous DNS resolution
- Asynchronous file and file system operations
- File system events
- IPC with socket sharing, using Unix domain sockets or named pipes (Windows)
- Child processes
- Thread pool
Concepts: Modules
How Modules are loaded?
- Global always overrides the local
- Looks for module in node_modules folder
- Traverses up the directory hierarchy
- Looks for *.js, *.json & *.node
Concepts: Callbacks
function slowAlert() {
alert("That was really slow!");
}
.
.
.
timeoutID = window.setTimeout(slowAlert, 2000);
- Callback is a parameter to a function that happens to be a function itself!
Concepts: Callbacks
- All APIs of Node are written is such a way that they supports callbacks.
- In Node.js a callback function is called at the completion of a given task.
- Node.js uses a callback concept called "error-first callbacks".
fs.readFile('input.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
Concepts: Events
- Node.js is coded around events
- You start your server, initiate most of the variables, declare your functions and then just wait for an event to occur.
- There are lots of built-in events
- You have the ability to create events using events module
var events = require('events');
var eventEmitter = new events.EventEmitter();
var sayHello = function() {
console.log('Hello there..');
}
eventEmitter.on('sayHello', sayHello);
eventEmitter.emit('sayHello');
Pros & Cons
Pros of using Node.js
- Fast - Execution Speed
- ALM turnaround is small
- Great Community
- Proven to handle high volume systems
- Most suited for networking applications
- Most suited for I/O intensive applications
Pros & cons
Cons of using Node.js
- Writing event driven code have a learning curve
- Writing huge business centric systems
- Callback hell
- Exception handling is not straight forward
Best Scenarios for using Node.js
- Most suited for networking & I/O intensive applications
- Application that need high concurrency & queued concurrent inputs
- Streaming applications like media streaming, chat applications
- Proxy service
- Real time applications
- Dashboard applications
- Exposing REST APIs
REPL
- REPL - Read, Evaluate, Print, Loop
- It is the Node.js shell
- It provides a way to interactively run JavaScript and see the results
- REPL can be started by simply running node on shell/console without any argument
Demos
- REPL hands on!
- Hello World Application
- Modules
- A Simple Web server
How can we make use of Node.js
- Hosting Static files
- Automation libraries like gulp, grunt, sass or css compilers, bower etc
- Build automation and productivity tools for internal use
Node.js
An Introduction to get you started!
Created by Abdel Raoof Olakara