Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Introduction to nodejs

Node js

Uploaded by

Kp
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction to nodejs

Node js

Uploaded by

Kp
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Agenda

• What is Node.Js? What is a multipage application?


• Installation of Node.Js and NPM
• npm tool, Yarn and npx
• Create a node project.
• npm init
• Project execution
• Package.json
• Modules
• What are modules? Types of modules.
• Main modules: HTTP, FS, OS, Path and URL.
• Process.agrv property
Full stack
Front end
[signup/register page /dashboard] => UI only => User Interface

Backend language
[fetching and manipulating data from db] =>api => application program interface

database (DB)
where we store all the data like users, products etc

Signup(example)
Front end (UI-react) -> frontend page(sign up page) -> submit button -> /signup api call (fetch,axios,http)
Backend (Node) -> receive /signup request -> save data in db -> send success response
Database(Mongo) -> connected with node for CRUD

you upload file


profile picture of abc
server -> (profile pic) -> id.timestamp.type -> name of pic
What is Node.Js ?
• asynchronous
• event-driven
• JavaScript runtime (v8 engine)
• Node.js is free
• cross-platform Node.js runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
Single page vs multi page
Features of NodeJs
1)Asynchronous in Nature
2)Single Threaded Architecture->( Node.js runs in one process and doesn’t create
new threads for every request.
Node.js operates as a single-threaded application, meaning it runs in one main
process rather than creating multiple processes or threads to handle requests.
In traditional server environments (e.g., Java or PHP), every incoming request may
create a new thread or process, which consumes more system resources.)

3)Scalable
4)Very Fast
5)No Buffering − Node.js applications never buffer any data. These applications
simply output the data in chunks.
Installation of Node.Js and NPM:

https://nodejs.org/en/download/
To confirm Node installation, type node -v command.

To confirm NPM installation, type npm -v command.


Npm – (node package manager)
npm
NPM
• NPM is a package manager for Node.js packages, or modules if you
like.
• www.npmjs.com hosts thousands of free packages to download and
use.
• A package in Node.js contains all the files you need for a module.
(reusable)
• npm init
package.json- The package. json file
is the heart of any Node project.
•name: the name of your JavaScript library/project
•version: the version of your project.
it can come handy as a source of the deployment's
version.
•description: the project's description
•license: the project's license

•npm init -f
YARN: NPM:NPX:
• Similar to npm
• Earlier it was fast
• npm install --global yarn
• Yarn uses the yarn command to install dependencies. It installs
dependencies in parallel, allowing you to add multiple files at the
same time.

• Similarly NPX: Node Package Runner


npx cowsay "Hello"
Creation of a Node project:
• mkdir myapp
• cd myapp
• npm init
Types of Modules in Node.Js
There are three types of modules
Core Modules -> belong to node js -> HTTP, FS, OS, Path and URL.
local Modules -> you created for your self -> reusable
Third-party Modules -> express mongoose moment react [npm]

Core Modules
let module = require('module_name') HTTP, FS, OS, Path and URL.

//local Modules (sum of 2 no)


exports.add = function(n,m){
return n+m;
};
let sum = require('./sum')
console.log("Sum of 10 and 20 is ", sum.add(10, 20))
• Modules that are available online and are installed using the npm are
called third party modules.
• Examples of third party modules are express, mongoose, etc.
File System Module:

• Reading File
• fs.readFile(fileName [,options], callback)
• fs.readFileSync()
• Write File
• fs.writeFile(filename, data[, options], callback)
• fs.appendFile() method to append the content to an existing file.
• OpenFile
• fs.open(path, flags[, mode], callback)
const fs = require('fs');

fs.writeFile('test.txt', 'Hello World!', function (err) {


Write file
if (err)
console.log(err);//throwing new Error(‘unable to create”);
else
console.log('Write operation complete.');
});
fs.readFile('test.txt', function (xerr, data) {
if (err) throw err;
console.log(data);
console.log(data.toString());
});
var data = fs.readFileSync('test.txt', 'utf8');
console.log(data);
fs.appendFile('test.txt', 'Hello World!', function (err) {
if (err)
console.log(err);
else
console.log('Append operation complete.');
Path Module:

const path = require('path');


path.join('/path', 'to', 'test.txt'); // '/path/to/test.txt‘;
'/path' + '/' + 'to' + '/' + 'test.txt'; // '/path/to/test.txt'
['/path', 'to', 'test.txt'].join('/'); // '/path/to/test.txt‘
console.log(path.join('data', 'test.txt')); // 'data/test.txt'
console.log(path.join('data', '/test.txt')); // 'data/test.txt'
console.log(path.join('data/', 'test.txt')); // 'data/test.txt'
console.log(path.join('data/', '/test.txt')); // 'data/test.txt'
URL Module:
• var url = require('url');
• var adr = 'http://localhost:8080/default.htm?
year=2017&month=february';
• var q = url.parse(adr, true);
• console.log(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //returns '?year=2017&month=february'
• var qdata = q.query; //returns an object: { year: 2017, month:
'february' }
• console.log(qdata.month); //returns 'february'
OS Module:
• const os = require('os');
• let currentOS = {
name: os.type(),
architecture: os.arch(),
platform: os.platform(),
release: os.release(),
version: os.version()
};
console.log(currentOS);
• console.log(os.userInfo());
Node.Js argv property:
• const process = require('process');
• console.log(process.argv);
• console.log(process.argv[2]);

You might also like