SlideShare a Scribd company logo
node essentials.js
Made with by @elacheche_bedis
About me
Bedis ElAcheche (@elacheche_bedis)
- Lazy web & mobile developer
- Official Ubuntu Member
- FOSS lover
- Javascript fanatic
- I’m writing bugs into apps since 2008
What to expect ahead..
- Data types
- Variables
- Conditional statements
- Loops
- Functions
- Comments
What to expect ahead..
What to expect ahead (for real)...
- Introduction
- Event loops
- Blocking and nonblocking I/O
- Modules
- NPM
- Demos
- Random memes
What is Node.js?
- Open source, cross platform development platform.
- A command line tool.
- Created by Ryan Dahl in 2009.
- Uses v8 JavaScript engine.
- Uses an event-driven, non-blocking I/O model.
Why Node.js?
- Free
- Open source
- Very lightweight and fast because it's mostly C/C++ code.
- Can handle thousands of concurrent connections with minimal overhead
(CPU/Memory).
- There are lots of modules available for free.
When to use Node.js?
- HTTP servers.
- Chat applications.
- Online games.
- Collaboration tools.
- Desktop applications.
- If you are great at writing javascript code.
When not to use Node.js?
- Heavy and CPU intensive calculations on server side.
- Node.js is single thread
- You have to write logic by your own to utilize multi core processor and make it
multi threaded.
Who uses Node.js?
- LinkedIn
- Microsoft
- Netflix
- PayPal
- SAP
- Walmart
- Uber
- ...
Event loops
- The core of event-driven programming.
- Almost all the UI programs use event loops to track the user event.
- Register callbacks for events.
- Your callback is eventually red.fi
Event loops
$('a').click(function() {
console.log('clicked!');
});
$.get('slides.php', {
from: 1,
to: 5
}, function(data) {
console.log('New slides!');
});
Event loops life cycle
Event loop
(single thread)
Register callback
Trigger callback Operation
complete
Requests
File System
Database
Network
Event loops life cycle
- Initialize empty event loop.
- Execute non-I/O code.
- Add every I/O call to the event loop.
- End of source code reached.
- Event loop starts iterating over a list of events and callbacks.
- Perform I/O using non-blocking kernel facilities.
- Event loop goes to sleep.
- Kernel noti es the event loop.fi
- Event loop executes and removes a callback.
- Program exits when event loop is empty.
Blocking and nonblocking I/O
// Blocking I/O
var attendees = db.query('SELECT * FROM attendees'); // Wait for result!
attendees.each(function(attendee) {
var attendeeName = attendee.getName(); // Wait for result!
sayHello(attendeeName); // Wait
});
startWorkshop(); // Execution is blocked!
Blocking and nonblocking I/O
// Nonblocking I/O
db.query('SELECT * FROM attendees', function(attendees) {
attendees.each(function(attendee) {
attendee.getName(function(attendeeName) {
sayHello(attendeeName);
});
});
});
startWorkshop(); // Executes without any delay!
Blocking and nonblocking I/O
Events in Node.js
- An action detected by the program that may be handled by the program.
- Determine which function will be called next.
- Many objects in node emit events.
- You can create objects that emit events too.
Events in Node.js
// Custom event emitters
var EventEmitter = require('events');
var logger = new EventEmitter();
logger.on('new-attendee', function(message) {
console.log('Welcome %s! Please have a seat.', message);
});
logger.on('attendee-left', function(message) {
console.log('Goodbye %s! See you next time.', message);
});
logger.emit('new-attendee', 'John Doe');
// Welcome John Doe! Please have a seat.
logger.emit('new-attendee', 'Jane Doe');
// Welcome Jane Doe! Please have a seat.
logger.emit('attendee-left', 'John Smith');
// Goodbye John Smith! See you next time.
Events in Node.js
Node.js modules
- External libraries.
- One kind of package which can be published to NPM.
- Node.js heavily relies on modules.
- Creating a module is easy, just put your javascript code in a separate js file
and include it in your code by using keyword require.
Node.js modules
/* Hello world */
var aModule = require('module1');
var otherModule = require('module2');
/* Awesome code */
/* Cool stuff */
app.js
./node_modules
module1.js module2.js
Node.js modules
// greetings.js
var hello = function() {
console.log('Hello %s!', name || '');
}
var bye = function() {
console.log('Bye %s!', name || '');
}
module.exports = {
hello: hello,
bye: bye
};
// hola.js
module.exports = function(name) {
console.log('Hola %s!', name || '');
};
// app.js
var greetings = require('./greetings');
var hola = require('./hola');
greetings.hello();
// Hello!
hola('John doe');
// Hola Jane doe!
// If we only need to call once
require('./greetings').bye('Jane');
// Goodbye Jane!
Node.js modules
Node.js modules
How does require return the libraries?// look in same directory
var awesomeModule = require('./awesome-module')
// look in parent directory
var awesomeModule = require('../awesome-module')
// look in specific directory
var awesomeModule = require('/home/d4rk-5c0rp/lab/awesome-module');
// app.js located under /home/d4rk-5c0rp/lab/nodeWorkshop
var awesomeModule = require('awesome-module');
// search in node_modules directories
// /home/d4rk-5c0rp/lab/nodeWorkshop/node_modules
// /home/d4rk-5c0rp/lab/node_modules
// /home/d4rk-5c0rp/node_modules
// /home/node_modules
// /node_modules
NPM
- Package manager for node.
- Comes bundled with Node.js installation.
- Command line client that interacts with a remote registry.
- Allows users to consume and distribute JavaScript modules.
- Manage packages that are local dependencies of a particular project.
- Manage as well globally-installed JavaScript tools.
NPM registry
- Kind of an App store for developers.
- There are a whole lot of modules and tools available to make the process of
building programs quicker and simpler.
- Look up the functionality you want, and hopefully found a module that does it
for you.
Installing a NPM module
npm install
npm install <tarball file>
npm install <tarball url>
npm install <name>
npm install <name>@<tag>
npm install <name>@<version>
npm install <name> [--save|--save-dev]
Install modules into local node_modules directory
If a module requires another module to operate, NPM will download
automatically!
Installing a NPM module
npm install gulp -g
Install modules with executables globally
Global NPM modules CAN’T be required
var gulp = require('gulp');
// Error: Cannot find module 'gulp'
Express.js
- Minimal and flexible Node.js framework.
- Free and open-source.
- Designed for building web applications and APIs.
Express.js
Socket.IO
- Realtime application framework.
- Enables bidirectional event-based communication.
- It has two parts: a client-side library that runs in the browser, and a server-side
library for Node.js.
Socket.IO
slides.emit('Thank you!');
Made with by @elacheche_bedis

More Related Content

What's hot (20)

PDF
Node.js Explained
Jeff Kunkle
 
PDF
Original slides from Ryan Dahl's NodeJs intro talk
Aarti Parikh
 
PPT
RESTful API In Node Js using Express
Jeetendra singh
 
PPTX
Java script at backend nodejs
Amit Thakkar
 
PDF
Node ppt
Tamil Selvan R S
 
PPTX
NodeJS - Server Side JS
Ganesh Kondal
 
KEY
OSCON 2011 - Node.js Tutorial
Tom Croucher
 
PPTX
Introduction to Node js
Akshay Mathur
 
PPTX
Nodejs getting started
Triet Ho
 
PPTX
Nodejs intro
Ndjido Ardo BAR
 
PPTX
Node.js Patterns for Discerning Developers
cacois
 
PPT
Nodejs Event Driven Concurrency for Web Applications
Ganesh Iyer
 
PPTX
Introduction Node.js
Erik van Appeldoorn
 
PPT
Introduction to node.js aka NodeJS
JITENDRA KUMAR PATEL
 
ODP
SockJS Intro
Ngoc Dao
 
PPTX
Introduction to node.js
Dinesh U
 
KEY
Building a real life application in node js
fakedarren
 
PDF
NodeJS for Beginner
Apaichon Punopas
 
PDF
Understanding the Node.js Platform
Domenic Denicola
 
KEY
A million connections and beyond - Node.js at scale
Tom Croucher
 
Node.js Explained
Jeff Kunkle
 
Original slides from Ryan Dahl's NodeJs intro talk
Aarti Parikh
 
RESTful API In Node Js using Express
Jeetendra singh
 
Java script at backend nodejs
Amit Thakkar
 
NodeJS - Server Side JS
Ganesh Kondal
 
OSCON 2011 - Node.js Tutorial
Tom Croucher
 
Introduction to Node js
Akshay Mathur
 
Nodejs getting started
Triet Ho
 
Nodejs intro
Ndjido Ardo BAR
 
Node.js Patterns for Discerning Developers
cacois
 
Nodejs Event Driven Concurrency for Web Applications
Ganesh Iyer
 
Introduction Node.js
Erik van Appeldoorn
 
Introduction to node.js aka NodeJS
JITENDRA KUMAR PATEL
 
SockJS Intro
Ngoc Dao
 
Introduction to node.js
Dinesh U
 
Building a real life application in node js
fakedarren
 
NodeJS for Beginner
Apaichon Punopas
 
Understanding the Node.js Platform
Domenic Denicola
 
A million connections and beyond - Node.js at scale
Tom Croucher
 

Similar to Node.js essentials (20)

PDF
Introduction to nodejs
James Carr
 
PPT
Nodejs Intro Part One
Budh Ram Gurung
 
PDF
Nodejs vatsal shah
Vatsal N Shah
 
PPTX
Introduction to node.js By Ahmed Assaf
Ahmed Assaf
 
PDF
Tech io nodejs_20130531_v0.6
Ganesh Kondal
 
PDF
Basic Understanding and Implement of Node.js
Gary Yeh
 
PPTX
Proposal
Constantine Priemski
 
PDF
UNIT-3.pdf, buffer module, treams,file accessing using node js
chandrapuraja
 
PPTX
Introduction to node.js GDD
Sudar Muthu
 
PPTX
Basic Concept of Node.js & NPM
Bhargav Anadkat
 
PPTX
Introduction to node.js by jiban
Jibanananda Sana
 
PPTX
A complete guide to Node.js
Prabin Silwal
 
PPTX
node.js.pptx
rani marri
 
PDF
Node.js 101 with Rami Sayar
FITC
 
PDF
Node.js introduction
Prasoon Kumar
 
PPTX
An overview of node.js
valuebound
 
DOCX
unit 2 of Full stack web development subject
JeneferAlan1
 
PDF
FITC - Node.js 101
Rami Sayar
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Introduction to nodejs
James Carr
 
Nodejs Intro Part One
Budh Ram Gurung
 
Nodejs vatsal shah
Vatsal N Shah
 
Introduction to node.js By Ahmed Assaf
Ahmed Assaf
 
Tech io nodejs_20130531_v0.6
Ganesh Kondal
 
Basic Understanding and Implement of Node.js
Gary Yeh
 
UNIT-3.pdf, buffer module, treams,file accessing using node js
chandrapuraja
 
Introduction to node.js GDD
Sudar Muthu
 
Basic Concept of Node.js & NPM
Bhargav Anadkat
 
Introduction to node.js by jiban
Jibanananda Sana
 
A complete guide to Node.js
Prabin Silwal
 
node.js.pptx
rani marri
 
Node.js 101 with Rami Sayar
FITC
 
Node.js introduction
Prasoon Kumar
 
An overview of node.js
valuebound
 
unit 2 of Full stack web development subject
JeneferAlan1
 
FITC - Node.js 101
Rami Sayar
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Ad

Recently uploaded (20)

PDF
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
PDF
SMART HOME AUTOMATION PPT BY - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
PDF
Natural Language processing and web deigning notes
AnithaSakthivel3
 
PDF
A NEW FAMILY OF OPTICALLY CONTROLLED LOGIC GATES USING NAPHTHOPYRAN MOLECULE
ijoejnl
 
PPTX
Smart_Cities_IoT_Integration_Presentation.pptx
YashBhisade1
 
PDF
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
PDF
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
PPTX
00-ClimateChangeImpactCIAProcess_PPTon23.12.2024-ByDr.VijayanGurumurthyIyer1....
praz3
 
PDF
July 2025 - Top 10 Read Articles in Network Security & Its Applications.pdf
IJNSA Journal
 
PDF
3.-Differential-Calculus-Part-2-NOTES.pdf
KurtMarbinCalicdan1
 
PDF
1_ISO Certifications by Indian Industrial Standards Organisation.pdf
muhammad2010960
 
PPT
04 Origin of Evinnnnnnnnnnnnnnnnnnnnnnnnnnl-notes.ppt
LuckySangalala1
 
PPTX
Dolphin_Conservation_AI_txhasvssbxbanvgdghng
jeeaspirant2026fr
 
PPTX
GitHub_Copilot_Basics...........................pptx
ssusera13041
 
PPTX
Mining Presentation Underground - Copy.pptx
patallenmoore
 
PDF
th International conference on Big Data, Machine learning and Applications (B...
Zac Darcy
 
PDF
mosfet introduction engg topic for students.pdf
trsureshkumardata
 
PDF
Web Technologies - Chapter 3 of Front end path.pdf
reemaaliasker
 
PPTX
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
PPTX
Fluid statistics and Numerical on pascal law
Ravindra Kolhe
 
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
SMART HOME AUTOMATION PPT BY - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
Natural Language processing and web deigning notes
AnithaSakthivel3
 
A NEW FAMILY OF OPTICALLY CONTROLLED LOGIC GATES USING NAPHTHOPYRAN MOLECULE
ijoejnl
 
Smart_Cities_IoT_Integration_Presentation.pptx
YashBhisade1
 
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
00-ClimateChangeImpactCIAProcess_PPTon23.12.2024-ByDr.VijayanGurumurthyIyer1....
praz3
 
July 2025 - Top 10 Read Articles in Network Security & Its Applications.pdf
IJNSA Journal
 
3.-Differential-Calculus-Part-2-NOTES.pdf
KurtMarbinCalicdan1
 
1_ISO Certifications by Indian Industrial Standards Organisation.pdf
muhammad2010960
 
04 Origin of Evinnnnnnnnnnnnnnnnnnnnnnnnnnl-notes.ppt
LuckySangalala1
 
Dolphin_Conservation_AI_txhasvssbxbanvgdghng
jeeaspirant2026fr
 
GitHub_Copilot_Basics...........................pptx
ssusera13041
 
Mining Presentation Underground - Copy.pptx
patallenmoore
 
th International conference on Big Data, Machine learning and Applications (B...
Zac Darcy
 
mosfet introduction engg topic for students.pdf
trsureshkumardata
 
Web Technologies - Chapter 3 of Front end path.pdf
reemaaliasker
 
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
Fluid statistics and Numerical on pascal law
Ravindra Kolhe
 
Ad

Node.js essentials

  • 1. node essentials.js Made with by @elacheche_bedis
  • 2. About me Bedis ElAcheche (@elacheche_bedis) - Lazy web & mobile developer - Official Ubuntu Member - FOSS lover - Javascript fanatic - I’m writing bugs into apps since 2008
  • 3. What to expect ahead.. - Data types - Variables - Conditional statements - Loops - Functions - Comments
  • 4. What to expect ahead..
  • 5. What to expect ahead (for real)... - Introduction - Event loops - Blocking and nonblocking I/O - Modules - NPM - Demos - Random memes
  • 6. What is Node.js? - Open source, cross platform development platform. - A command line tool. - Created by Ryan Dahl in 2009. - Uses v8 JavaScript engine. - Uses an event-driven, non-blocking I/O model.
  • 7. Why Node.js? - Free - Open source - Very lightweight and fast because it's mostly C/C++ code. - Can handle thousands of concurrent connections with minimal overhead (CPU/Memory). - There are lots of modules available for free.
  • 8. When to use Node.js? - HTTP servers. - Chat applications. - Online games. - Collaboration tools. - Desktop applications. - If you are great at writing javascript code.
  • 9. When not to use Node.js? - Heavy and CPU intensive calculations on server side. - Node.js is single thread - You have to write logic by your own to utilize multi core processor and make it multi threaded.
  • 10. Who uses Node.js? - LinkedIn - Microsoft - Netflix - PayPal - SAP - Walmart - Uber - ...
  • 11. Event loops - The core of event-driven programming. - Almost all the UI programs use event loops to track the user event. - Register callbacks for events. - Your callback is eventually red.fi
  • 12. Event loops $('a').click(function() { console.log('clicked!'); }); $.get('slides.php', { from: 1, to: 5 }, function(data) { console.log('New slides!'); });
  • 13. Event loops life cycle Event loop (single thread) Register callback Trigger callback Operation complete Requests File System Database Network
  • 14. Event loops life cycle - Initialize empty event loop. - Execute non-I/O code. - Add every I/O call to the event loop. - End of source code reached. - Event loop starts iterating over a list of events and callbacks. - Perform I/O using non-blocking kernel facilities. - Event loop goes to sleep. - Kernel noti es the event loop.fi - Event loop executes and removes a callback. - Program exits when event loop is empty.
  • 15. Blocking and nonblocking I/O // Blocking I/O var attendees = db.query('SELECT * FROM attendees'); // Wait for result! attendees.each(function(attendee) { var attendeeName = attendee.getName(); // Wait for result! sayHello(attendeeName); // Wait }); startWorkshop(); // Execution is blocked!
  • 16. Blocking and nonblocking I/O // Nonblocking I/O db.query('SELECT * FROM attendees', function(attendees) { attendees.each(function(attendee) { attendee.getName(function(attendeeName) { sayHello(attendeeName); }); }); }); startWorkshop(); // Executes without any delay!
  • 18. Events in Node.js - An action detected by the program that may be handled by the program. - Determine which function will be called next. - Many objects in node emit events. - You can create objects that emit events too.
  • 19. Events in Node.js // Custom event emitters var EventEmitter = require('events'); var logger = new EventEmitter(); logger.on('new-attendee', function(message) { console.log('Welcome %s! Please have a seat.', message); }); logger.on('attendee-left', function(message) { console.log('Goodbye %s! See you next time.', message); }); logger.emit('new-attendee', 'John Doe'); // Welcome John Doe! Please have a seat. logger.emit('new-attendee', 'Jane Doe'); // Welcome Jane Doe! Please have a seat. logger.emit('attendee-left', 'John Smith'); // Goodbye John Smith! See you next time.
  • 21. Node.js modules - External libraries. - One kind of package which can be published to NPM. - Node.js heavily relies on modules. - Creating a module is easy, just put your javascript code in a separate js file and include it in your code by using keyword require.
  • 22. Node.js modules /* Hello world */ var aModule = require('module1'); var otherModule = require('module2'); /* Awesome code */ /* Cool stuff */ app.js ./node_modules module1.js module2.js
  • 23. Node.js modules // greetings.js var hello = function() { console.log('Hello %s!', name || ''); } var bye = function() { console.log('Bye %s!', name || ''); } module.exports = { hello: hello, bye: bye }; // hola.js module.exports = function(name) { console.log('Hola %s!', name || ''); }; // app.js var greetings = require('./greetings'); var hola = require('./hola'); greetings.hello(); // Hello! hola('John doe'); // Hola Jane doe! // If we only need to call once require('./greetings').bye('Jane'); // Goodbye Jane!
  • 25. Node.js modules How does require return the libraries?// look in same directory var awesomeModule = require('./awesome-module') // look in parent directory var awesomeModule = require('../awesome-module') // look in specific directory var awesomeModule = require('/home/d4rk-5c0rp/lab/awesome-module'); // app.js located under /home/d4rk-5c0rp/lab/nodeWorkshop var awesomeModule = require('awesome-module'); // search in node_modules directories // /home/d4rk-5c0rp/lab/nodeWorkshop/node_modules // /home/d4rk-5c0rp/lab/node_modules // /home/d4rk-5c0rp/node_modules // /home/node_modules // /node_modules
  • 26. NPM - Package manager for node. - Comes bundled with Node.js installation. - Command line client that interacts with a remote registry. - Allows users to consume and distribute JavaScript modules. - Manage packages that are local dependencies of a particular project. - Manage as well globally-installed JavaScript tools.
  • 27. NPM registry - Kind of an App store for developers. - There are a whole lot of modules and tools available to make the process of building programs quicker and simpler. - Look up the functionality you want, and hopefully found a module that does it for you.
  • 28. Installing a NPM module npm install npm install <tarball file> npm install <tarball url> npm install <name> npm install <name>@<tag> npm install <name>@<version> npm install <name> [--save|--save-dev] Install modules into local node_modules directory If a module requires another module to operate, NPM will download automatically!
  • 29. Installing a NPM module npm install gulp -g Install modules with executables globally Global NPM modules CAN’T be required var gulp = require('gulp'); // Error: Cannot find module 'gulp'
  • 30. Express.js - Minimal and flexible Node.js framework. - Free and open-source. - Designed for building web applications and APIs.
  • 32. Socket.IO - Realtime application framework. - Enables bidirectional event-based communication. - It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js.