際際滷

際際滷Share 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
- Im 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.鍖
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.鍖
- 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 CANT 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
Node ppt
Tamil Selvan R S
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
PDF
Node.js Course 1 of 2 - Introduction and first steps
Manuel Eusebio de Paz Carmona
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
Node.js Course 1 of 2 - Introduction and first steps
Manuel Eusebio de Paz Carmona
Ad

Recently uploaded (20)

PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
PPTX
Work at Height training for workers .pptx
cecos12
PDF
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
PDF
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
PPTX
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
PDF
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
PPTX
Introduction to Python Programming Language
merlinjohnsy
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
PPTX
Functions in Python Programming Language
BeulahS2
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
PPTX
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
PDF
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
PPTX
Mobile database systems 20254545645.pptx
herosh1968
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
Work at Height training for workers .pptx
cecos12
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
Computer network Computer network Computer network Computer network
Shrikant317689
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
FINAL plumbing code for board exam passer
MattKristopherDiaz
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
Introduction to Python Programming Language
merlinjohnsy
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
Functions in Python Programming Language
BeulahS2
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
Mobile database systems 20254545645.pptx
herosh1968
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 - Im 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.鍖
  • 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.鍖 - 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 CANT 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.