learning node - usmanlivefeb 01, 2019 · node recap node is a runtime environment for executing js...
TRANSCRIPT
![Page 1: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/1.jpg)
Learning Node MUHAMMAD USMAN AKRAM
![Page 2: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/2.jpg)
Node Getting StartedA run Time Environment for Executing Java Script Code. Mostly we build ….
CUI LAHORE
![Page 3: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/3.jpg)
CUI LAHORE
![Page 4: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/4.jpg)
CUI LAHORE
![Page 5: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/5.jpg)
Why NodePaypal, Uber, NetFlix, Wall Mart
CUI LAHORE
![Page 6: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/6.jpg)
CUI LAHORE
![Page 7: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/7.jpg)
Node Architecture
CUI LAHORE
![Page 8: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/8.jpg)
Node Architecture
CUI LAHORE
![Page 9: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/9.jpg)
Node Architecture by Ryan Dahl
CUI LAHORE
![Page 10: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/10.jpg)
JSONJAVASCRIPT OBJECT NOTATION
![Page 11: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/11.jpg)
Object{
"name": "Jack B. Nimble",
"at large": true,
"grade": "A",
}
CUI LAHORE
![Page 12: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/12.jpg)
Node Architecture by Ryan Dahl
CUI LAHORE
![Page 13: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/13.jpg)
Not suitable for computational Hungry Tasks
CUI LAHORE
![Page 14: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/14.jpg)
CUI LAHORE
![Page 15: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/15.jpg)
node app.jsfunction sayHello(name) {
console.log("Welcome " + name);
}sayHello("Usman");
//window or document objects are not available here
CUI LAHORE
![Page 16: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/16.jpg)
Node RecapNode is a runtime environment for executing JS code. –
Essentially, Node is a C++ program that embeds Chrome’s v8 engine, the fastest JS engine in the world. –
We use Node to build fast and scalable networking applications. It’s a perfect choice for building RESTfulservices. –
Node applications are single-threaded. That means a single thread is used to serve all clients. –
CUI LAHORE
![Page 17: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/17.jpg)
Node RecapNode applications are asynchronous or non-blocking by default. That means when the application involves I/O operations (eg accessing the file system or the network), the thread doesn’t wait (or block) for the result of the operation. It is released to serve other clients. –
This architecture makes Node ideal for building I/O-intensive applications. –
You should avoid using Node for CPU-intensive applications, such as a video encoding service. Because while executing these operations, other clients have to wait for the single thread to finish its job and be ready to serve them. –
In Node, we don’t have browser environment objects such as window or the document object. Instead, we have other objects that are not available in browsers, such as objects for working with the file system, network, operating system, etc.
CUI LAHORE
![Page 18: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/18.jpg)
NodeMODULE SYSTEM
![Page 19: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/19.jpg)
Window object is not available here
CUI LAHORE
![Page 20: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/20.jpg)
Modularity
CUI LAHORE
![Page 21: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/21.jpg)
Every file is a module
CUI LAHORE
![Page 22: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/22.jpg)
Logger.js exporting log function and url//paraneters available here
//exports,require, module, __filename,__dirnamefunction log(message) {
console.log(message);
}
var url="usmanlive.com";
console.log(__filename);
console.log(__dirname);module.exports.log = log;
module.exports.url = url;
CUI LAHORE
![Page 23: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/23.jpg)
Using logger.jsconst logger = require ('./logger');
logger.log("Hello Usman");
CUI LAHORE
![Page 24: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/24.jpg)
Exercise Install jshint◦ npm install –g jshint
◦ jshint app.js
Head to nodejs.org and explore docs
CUI LAHORE
![Page 25: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/25.jpg)
Built in modules (path)const path = require('path');
const pathObj = path.parse(__dirname);
console.log(pathObj);
CUI LAHORE
![Page 26: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/26.jpg)
Built in modules (fs)const fs = require('fs');
//sync should be avoided at all cost
//console.log(fs.readdirSync("./"));
files = fs.readdir('./',functionname(err,result) {
if(result) console.log(result);
});
console.log(files);
CUI LAHORE
![Page 27: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/27.jpg)
Built in modules (os)const os = require('os');
console.log(os.freemem());
console.log(os.uptime());
//template string available with ES6
console.log(`Home Directory ${os.homedir}`);
CUI LAHORE
![Page 28: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/28.jpg)
Built in modules (os)const fs = require('fs');
//sync should be avoided at all cost
//console.log(fs.readdirSync("./"));
files = fs.readdir('./',functionname(err,result) {
if(result) console.log(result);
});
console.log(files);
CUI LAHORE
![Page 29: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/29.jpg)
Node Eventsconst EventEmitter = require('events');
const emitter = new EventEmitter();
CUI LAHORE
![Page 30: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/30.jpg)
Here’s a prototype-based class Userfunction User(name) {
this.name = name;
}
User.prototype.sayHi = function() {
alert(this.name);
}
let user = new User("John");
user.sayHi();
CUI LAHORE
![Page 31: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/31.jpg)
The “class” syntaxclass User {
constructor(name) {
this.name = name;
}
sayHi() {
alert(this.name);
}
}
let user = new User("John");
user.sayHi();
CUI LAHORE
![Page 32: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/32.jpg)
A MessageLogged Eventconst EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on("MessageLogged",function(args) {
console.log("Event Received",args);
})
emitter.emit('MessageLogged',{id:1,name:'hello'});
CUI LAHORE
![Page 33: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/33.jpg)
logger.jsconst EventEmitter = require('events');
class Logger extends EventEmitter {
log(message) {
console.log(message);
this.emit("MessageLogged"); //pass params also
}
}
module.exports = Logger;
CUI LAHORE
![Page 34: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/34.jpg)
app.jsconst Logger = require ('./logger');
var logger = new Logger();
logger.on('MessageLogged',function () {
console.log("Event captured");
});
logger.log("Log this mssage");
CUI LAHORE
![Page 35: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/35.jpg)
Node http moduleWHOALA WE ARE BUILDING API NOW
![Page 36: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/36.jpg)
http
CUI LAHORE
![Page 37: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/37.jpg)
First api call listener – localhost:3000const http = require('http');const server = http.createServer((req,res)=>{
if(req.url==='/'){
res.write(JSON.stringify([1,4,5,6]));
res.end();
}
});
server.listen(3000);
console.log('server started at 3000....');
CUI LAHORE
![Page 38: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/38.jpg)
ExerciseCreate a web server which should return dummy articles from url◦ Localhost:3000/api/articles
CUI LAHORE
![Page 39: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/39.jpg)
Convert JSON to stringvar arr = [ "John", "Peter", "Sally", "Jane" ];
var myJSON = JSON.stringify(arr);
CUI LAHORE
![Page 40: Learning Node - UsmanLiveFeb 01, 2019 · Node Recap Node is a runtime environment for executing JS code. – Essentially, Node is a C++ program that embeds Chrome’s v8 engine,](https://reader036.vdocuments.us/reader036/viewer/2022063021/5fe57f1c0753e211113cb628/html5/thumbnails/40.jpg)
Convert String to JSONvar text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';var obj = JSON.parse(text);obj.birth = new Date(obj.birth);
CUI LAHORE