Node.js essentials
-
Upload
bedis-elacheche -
Category
Engineering
-
view
476 -
download
0
Transcript of Node.js essentials
![Page 2: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/2.jpg)
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
![Page 3: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/3.jpg)
What to expect ahead..
- Data types
- Variables
- Conditional statements
- Loops
- Functions
- Comments
![Page 4: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/4.jpg)
What to expect ahead..
![Page 5: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/5.jpg)
What to expect ahead (for real)...
- Introduction
- Event loops
- Blocking and nonblocking I/O
- Modules
- NPM
- Demos
- Random memes
![Page 6: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/6.jpg)
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.
![Page 7: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/7.jpg)
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.
![Page 8: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/8.jpg)
When to use Node.js?
- HTTP servers.
- Chat applications.
- Online games.
- Collaboration tools.
- Desktop applications.
- If you are great at writing javascript code.
![Page 9: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/9.jpg)
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.
![Page 10: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/10.jpg)
Who uses Node.js?
- Microsoft
- Netflix
- PayPal
- SAP
- Walmart
- Uber
- ...
![Page 11: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/11.jpg)
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
![Page 12: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/12.jpg)
Event loops
$('a').click(function() {
console.log('clicked!');
});
$.get('slides.php', {
from: 1,
to: 5
}, function(data) {
console.log('New slides!');
});
![Page 13: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/13.jpg)
Event loops life cycle
Event loop
(single thread)
Register callback
Trigger callback Operation
complete
Requests
File System
Database
Network
![Page 14: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/14.jpg)
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.
![Page 15: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/15.jpg)
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!
![Page 16: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/16.jpg)
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!
![Page 17: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/17.jpg)
Blocking and nonblocking I/O
![Page 18: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/18.jpg)
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.
![Page 19: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/19.jpg)
Events in Node.js
// Custom event emittersvar 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.
![Page 20: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/20.jpg)
Events in Node.js
![Page 21: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/21.jpg)
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.
![Page 22: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/22.jpg)
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
![Page 23: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/23.jpg)
Node.js modules
// greetings.jsvar hello = function() { console.log('Hello %s!', name || '');}var bye = function() { console.log('Bye %s!', name || '');}module.exports = { hello: hello, bye: bye};
// hola.jsmodule.exports = function(name) { console.log('Hola %s!', name || '');};
// app.jsvar 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!
![Page 24: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/24.jpg)
Node.js modules
![Page 25: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/25.jpg)
Node.js modules
How does require return the libraries?// look in same directoryvar awesomeModule = require('./awesome-module')// look in parent directoryvar awesomeModule = require('../awesome-module')// look in specific directoryvar awesomeModule = require('/home/d4rk-5c0rp/lab/awesome-module');
// app.js located under /home/d4rk-5c0rp/lab/nodeWorkshopvar 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
![Page 26: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/26.jpg)
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.
![Page 27: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/27.jpg)
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.
![Page 28: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/28.jpg)
Installing a NPM module
npm installnpm 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!
![Page 29: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/29.jpg)
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'
![Page 30: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/30.jpg)
Express.js
- Minimal and flexible Node.js framework.
- Free and open-source.
- Designed for building web applications and APIs.
![Page 31: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/31.jpg)
Express.js
![Page 32: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/32.jpg)
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.
![Page 33: Node.js essentials](https://reader033.fdocuments.in/reader033/viewer/2022051406/58f9adab760da3da068b9ab9/html5/thumbnails/33.jpg)
Socket.IO