Node JS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 94

The Server-side

JavaScript
MEAN STACK – FULL STACK DEVELOPMENT

 Angular : It is a UI development part. It is the front-end framework


that runs the code in the browser.
 NodeJS: It is the runtime system that implements JavaScript(js) on
the back-end web application. It is a runtime environment capable of
running JavaScript on a machine.
 ExpressJS: It is the back-end application that runs on top of NodeJS. It
is a layered framework built on top of NodeJS that takes care of the
website’s back-end functionality and structure.
 MongoDB: It is used to store the data of back-end applications as
JSON files. It is a document-oriented No-SQL data store.
MERN STACK – FULL STACK DEVELOPMENT
 ReactJS : It is a UI development part. It is a library that facilitates creating
the UI components of single-page web applications.
 NodeJS: It is the runtime system that implements JavaScript(js) on the back-
end web application. It is a runtime environment capable of running
JavaScript on a machine.
 ExpressJS: It is the back-end application that runs on top of NodeJS. It is a
layered framework built on top of NodeJS that takes care of the website’s
back-end functionality and structure.
 MongoDB: It is used to store the data of back-end applications as JSON files. It
is a document-oriented No-SQL data store.
History - NodeJS Application
 Created by Ryan Dahl, first 2009 for only Linux.
 Sponsored by Joyent.
 June2011, Microsoft partnered with Joyent to help create
a native Windows version of Node.js.
Introduction to Node.JS
 V8 is an open source JavaScript engine
developed by Google. Its written in C++ and
used in Google chrome browser.
 Node.js run on V8.
 Official Website for NodeJS Application -
https://nodejs.org
 Open source & cross-platform project-It run on
Linux Systems and also run on Windows
Systems.
 Node.js = Runtime Environment + JavaScript
Library
NodeJS App - Introduction
Software required for Full Stack using MERN STACK !!

 Node.js platform (The Node.js binary installable)


 npm (Node Package Manager) or npx or nvm
 MongoDB and MongoDB Compass
 Visual Studio Code (IDE) or Notepad / Notepad++ (Text
editor) or Sublime Text.
Some Characteristics of NodeJS
 Build on Chrome's JavaScript runtime environment.
 Event driven & Non-blocking IO(by calling callback
function).
 Real time apps.
 Single threaded.
 Build in Async IO, HTTP/s, file-system etc.
 Ability
to run JavaScript on out-side of a browser(that
means on server).
 Act as a web server
Some points about Node.js
 In simple word node.js is “server-side JavaScript
platform”
 It’s a command line tool (CLI based )
 In“node.js” where “js” doesn’t mean that its solely
written in JavaScript.
 It’s 40% “js” and 60% “c++”.
Continue…
 node js uses an event-driven,non-blocking i/o model
which makes it lightweight.
 Program for node.js is written in javascript and
extension of a nodejs file is .js but it’s not the same
javascript which we are use to, because there is no
document Object Model(DOM) implementation
provided by Node.js
Example: The below mentioned statement is not valid
for NodeJS Application:-
document.getElementById(“elementId”);
How to download and install
Node.js
Download link :
 https://nodejs.org/en/
How to download and install VS code
 Download link :
 https://code.visualstudio.com/download
Verify installation status!!
 By using Command Prompt window
 node - -version or node –v
 and
 npm - - version or npm –v
Introduction to Node.js Basic
Program
MODULE - 1
Lets start with “Hello World” Program…
REPL Terminal Example
(Read Eval Print Loop)
Node.js - First Application
About : nodemon module
Working with built-in module and some features

 http Module
 File System ( fs ) Module
 Path Module
 OS Module
 Custom Module
 Template String syntax
 Arrow function
Following code creates an HTTP server and prints
‘Welcome to node.js Application’
About last slide block of code:
Components of a NodeJS application

 Import required module – We use the require directive to


load NodeJS modules.
 Create server – A server which responsible to responding
client after processing the client request. A server which will
listen to client’s requests similar to Apache HTTP Server.
 Read request and return response – The server
created in an earlier step will read the request made by the client
which can be a browser or a console and return the response.
Coding example – http module
Example-1 : ‘path’ module
Example-1 : ‘os’ module
Using ‘Template string’ ES6
feature
“upper-case” module
Example -1
Example : 2 -“upper-case” module
Official site for npm - https://www.npmjs.com/
Run && Output
Custom module
Example 2
Node.js - Global Objects
Some Examples of global
object

1) __filename
2) __dirname
3) setTimeout( callback , milliseconds)
4) clearTimeout(timer)
5) setInterval(callback, milliseconds)
Example -1
Example -2
Example -3
Node.js - File System
How to read a text (.txt) file!!!
How to write one file content into another file
File reading
Remove file from dir
If file is not present=> error
Create dir Sync way
Remove dir Sync way
Create dir and file Async way
Use of ‘fs’ module
Event Module
Event Module : Example 1
Event Module : Example 2
Event (‘util’ module) : Example 2
Read File Stream
Read File Stream
Example - 2
Example-1 (fs module)
var fs=require('fs');

//var readStream=fs.createReadStream('hello.txt','utf8');
var readStream=fs.createReadStream('hello.txt');
readStream.setEncoding('utf8');
var data='';
readStream.on('data',function(md) {
console.log('---------------Start----------------------');
data = data + md; // data += md;
//console.log(md);
});
readStream.on('end',function(md){
console.log(data);
console.log('----------------End-----------------------');
})
Data flow control using “writeStream”
object
Data flow control using Pipes
Deals with JSON in Node.js
JSON-
Realtime example of passing JSON DATA
JSON Value
JSON- key, value pair.
Key– always string format
Passing JSON Data to the Web Browser
Output
Passing JSON Data to the Web Browser
Example - 2
Node.js – Routing Concept
Routing Concept using NodeJS app
Example-1
Routing Example - 2
Node.js - Express
Framework
NodeJS - NPM
 Node Package Manager(NPM) Provides two main functionalities –
 Online repositories for node.js package/modules which are searchable on
search.nodejs.org
 Command line utility to install Node.js packages, do version management and
dependency management of Node.js packages.
 CLI Commands are:
 npm --version

 Simple syntax to install any Node.js module –


 npm install <Module Name>
 For Global installation -
 npm install express -g
NodeJS NPM
How to install “express”
Use of “-save” flag or command
 If you use “-save” flag along with your install
command,that will add your package with in package.json
file
After uninstall express module:
Next stage (dependencies are still there within
package.json
Just using “npm install” command
get full express module
Pakage.json file

 Whenever you distribute your project.


 It contain records about how many new module are being added
into your project.
 This file is the entry point.
 This file is basically a config file for every nodejs project
perspective
Use of “ –g “ flag
Working with “ –g” flag
 Location or path where global modules are stored.
 By using this command we can install it globally.
Introduction to express js
Example 1:express js
How to pass parameters in the url
Example -1
Parameters in the url –
express_js
Example -2
Parameters in the url –
express_js
Example -3
Node.js - Utility Modules
Some important topics
Continue…
MongoDB
Download link – required S/W
 Mongodb-download
 https://www.mongodb.com/download-center/community

 Mongodb- compass
 https://www.mongodb.com/download-center/compass?jmp=hero

 Postman app::
 https://www.getpostman.com/apps

 NPM Official Site


 https://www.npmjs.com/
Some of the popular Application
using node.js
 Yahoo!
 Linkedin
 eBay

You might also like