451 likes | 1.7k Views
This Edureka "What is Node.js" tutorial will help you to learn the Node.js fundamentals and how to create an application in Node.js. Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of server tools and applications. Below are the topics covered in this tutorial: <br><br>1) Client Server Architecture <br>2) Limitations of Multi u2013 Threaded Model <br>3) What is Node.js? <br>4) Features of Node.js <br>5) Node.js Installation <br>6) Blocking Vs. Non u2013 Blocking I/O <br>7) Creating Node.js Program <br>8) Node.js Modules
E N D
Agenda 1 2 Why Node.js? What is Node.js? 3 5 Demo Success Stories 4 Node.js Modules EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Why Node.js? EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Client Server Architecture Client Server Architecture: Interacts Request Interacts Response Interacts Server Databases Interacts Users Application EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Multi Thread Model Each request is handled by a separate thread Handle Request A thread A Request A Response A Handle Request B thread B Request B Response B Handle Request C thread C Request C Response C … … Databases Thread Clients Server EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Multi Thread Model Handling Request A A ▪ In multi-threaded model, for every request server creates a separate thread which handles that request … B C ▪ If a thread acquires a lock in the shared resource and it is ‘exclusive lock’, it will block other threads. Thread Databases Request Thread B C … are blocked EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Single Threaded Model ▪ Node.js is event driven, handling all requests asynchronously from single thread ▪ Almost no function in Node directly performs I/O, so the process never blocks A Thread Pool Databases File System B Event Loop (Single - threaded) Network Event Emitters Event Queue Process C Other Users EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Multi-Threaded vs Event Driven Multi-Threaded Asynchronous Event-driven Lock application / request with listener-workers threads Only one thread, which repeatedly fetches an event Using incoming-request model Using queue and then processes it Multithreaded server might block the request which might involve multiple events Manually saves state and then goes on to process the next event Using context switching No contention and no context switches Using multithreading environments where listener and workers threads are used frequently to take an incoming-request lock Using asynchronous I/O facilities (callbacks, not poll/select or O_NONBLOCK) environments EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Uber Story www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Uber Old Architecture PHP for Server-side scripting MySQL Database UBER Server Application Since PHP is a multithreaded language , each user’s request is handled in a separate thread • Reason was car dispatch operation was executed from multiple threads • Once one car is dispatched for a user, in between the same car get dispatched to another user • EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Uber New Architecture Real-time Logic Dispatch State (MongoDB) ❖ Well-suited for distributed systems that make a lot of network requests Dispatch (NodeJS) ❖ Errors can be addressed on the fly without requiring a restart Python ❖ Active open source community Persistent Storage (MySQL) Business Logic EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
What is Node.js ? EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
What is Node.js ? ▪ Node.js is an open source runtime environment for server-side and networking applications and is single threaded. ▪ Uses Google JavaScript V8 Engine to execute code. ▪ It is cross platform environment and can run on OS X, Microsoft Windows, Linux and FreeBSD. ▪ Provides an event driven architecture and non blocking I/O that is optimized and scalable. EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Success Stories www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Success Stories PayPal team developed the application simultaneously using Java and Javascript. The JavaScript team build the product both faster and more efficiently. Nexflix used JavaScript and NodeJS to transform their website into a single page application. Uber has built its massive driver / rider matching system on Node.js Distributed Web Architecture. When LinkedIn went to rebuild their Mobile application they used Node.js for their Mobile application server which acts as a REST endpoint for Mobile devices. They had two primary requirements: first to make the application as real time as possible. Second was to orchestrate a huge number of eBay-specific services. Node enables to build quality applications, deploy new features, write unit and integration tests easily. EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Job Trends www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Node.js Job Trend 2017 Indeed: Job Posting (Node.js) EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js Features EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js Features Asynchronous and Event Driven : request response Caller Recipient callback ▪ When request is made to server, instead of waiting for the request to complete, server continues to process other requests ▪ When request processing completes, the response is sent to caller using callback mechanism EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js Features Very Fast Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution. Fast Performance Single Threaded but Highly Scalable Uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers. No Buffering Node.js applications never buffer any data. These applications simply output the data in chunks. EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js Installation www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Node.js First Example www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Node.js First Example 1 Create a Directory: mkdir node_example 1 2 Create a File: first_example.js 2 3 Go to the node_example directory 3 Execute the java script file: node first_example.js 4 4 www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Blocking vs Non - Blocking www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Blocking vs Non-Blocking www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Blocking vs Non-Blocking ➢ Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. ➢ "I/O" refers primarily to interaction with the system's disk and network supported by libuv. Blocking I/O Non-Blocking I/O var fs = require("fs"); var fs = require("fs"); // Synchronous read var data = fs.readFileSync('input.txt'); // Asynchronous read fs.readFile(‘test.txt', function (err, data) { }); More methods will be blocked till the read method is not executed More method will execute asynchronously www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM GLOBALS FILE SYSTEM Node.js Modules CALLBACKS EVENT HTTP www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM NPM => Node Package Manager ▪ Provides online repositories for node.js packages/modules ▪ Provides command line utility to install Node.js packages ▪ Install all the modules as specified in package.json npm install npm install <Module Name> Install Module using npm npm install <Module Name> -g Install dependency globally www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM GLOBALS FILE SYSTEM Node.js Modules CALLBACKS EVENT HTTP www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Global Objects These objects are available in all modules specifies the name of the directory that currently contains the code. __dirname specifies the name of the file that currently contains the code. __filename A timer in Node.js is an internal construct that calls a given function after a certain period of time 1 2 3 setTimeout(callback, delay[, ...args]) setInterval(callback, delay[, ...args]) setImmediate(callback, [,..args]) www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM GLOBALS FILE SYSTEM Node.js Modules CALLBACKS EVENT HTTP www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
File System File I/O is provided by simple wrappers around standard POSIX functions var fs = require("fs"); FS Methods Synchronous Forms Asynchronous Forms Callback As Last Arg. var fs = require("fs"); var fs = require("fs"); // Synchronous read // Asynchronous read var data = fs.readFileSync('input.txt'); fs.readFile(‘test.txt', function (err, data) { }); www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Methods in File System Module Open File Asynchronously fs.open( path, flags[, mode], callback ) Open File Synchronously fs.openSync( path, flags[, mode] ) Opening a File Closing File fs.close( fd, callback ) Read Content of a File into Buffer read(fd, buffer, offset, length, position, callback) Reads File Asynchronously readFile(file[, options], callback) Reading from a file Reads File Synchronously readFileSync(file[, options]) www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Methods in File System Module Open File Asynchronously writeFile(file, data[, options], callback) Writing in a File Open File Synchronously writeFileSync(file, data[, options]) www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM GLOBALS FILE SYSTEM Node.js Modules CALLBACKS EVENT HTTP www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Callback Callback is an asynchronous equivalent for a function and is called at the completion of each task Callback: will execute after file read is complete var fs = require("fs"); // Asynchronous read fs.readFile(‘test.txt', function (err, data) { }); www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM GLOBALS FILE SYSTEM Node.js Modules CALLBACKS EVENT HTTP www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Events Node.js follows event-driven architecture ▪ Certain objects (emitters) periodically emit events which further invokes the listeners ▪ Node.js provide concurrency by using the concept of events and callbacks ▪ All objects that emit events are instances of the EventEmitter class ▪ Import Events Module Creating object of EventEmitter Emitting event Registering Listener and defining event handler www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
NPM GLOBALS FILE SYSTEM Node.js Modules CALLBACKS EVENT HTTP www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
HTTP To use the HTTP server and client one must require('http') ▪ The HTTP interfaces in Node.js are designed to support many features of the protocol ▪ Import Required Modules Creating Server Parse the fetched URL to get pathname Request file to be read from file system (index.html) Creating Header with content type as text or HTML Generating Response Listening to port: 3000 www.edureka.co/mastering-node-js EDUREKA NODE.JS CERTIFICATION TRAINING
Thank You … Questions/Queries/Feedback EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/angular-js