0% found this document useful (0 votes)
15 views

Node Js

Uploaded by

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

Node Js

Uploaded by

patilsiddeshb16
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Earlier only through browser we could run js files . now node.

js also
provides the similar environment to run js files.
Node REPL –
By typing node on terminal you enter the cli of node in which you can
execute js expressions and commands. Note that you cannot
manipulate dom here as it just provides env to run js.
To run js using node – go to file directory and type node filename.js
in terminal

Agar hum terminal me jake node type karke process type karenge to
wo process object show hoga.
Humne jo bhi arguments pass kiye hai .js file run karte time wo
Process.argv se dekh sakte hai. Process.argv hame ek array return
karta hai.Isme by default 2 parameters ate hai – 1. Node installed
directory. 2. cwd directory.
Module.exports ek object hota hai jo
ham baki files ko pass karte hai jisse
wo file doosre files ki methods,
parameters,etc use kar sake.

Agar ek file ko wo module chahiye to


use require(“./filename”) karna
padta hai.

e.g 1. Simple string export

Isme math.js sirf ek string de raha hai doosre files ko wo hum require
kar rahe hai script.js me.
e.g 2. Object export
Isme humne math.js ke sabhi
functions ko ek object me daala aur
wo obj ko export kia hai.
Isme script.js file math.js
ke exports ko require kar
rahi hai.

e.g 3. Shortcut to export

Export in directories –

Make an index.js which requires all the objects in the same directory.
Then it exports the object’s array. Index.js ek entry point require ke
liye. Node.js ek directory me sabse pehle index.js dekhega aur usi ke
exports ko require karega.

Then require the path of directory in other directory.

NPM –
Package code hota hai jo developers ne likha hota hai aur jise hum
use kar sakte hai. Hame pata hona chahiye ki package kaunse file ya
directory me install kiya hai kyuki woos package ka scope bas utna hi
hota hai.

Node_modules me sabhi
dependencies hoti hai.
Jaise ki humne install kiye
huye packages.
Node_modules apne aap
create hote ek package ko
install karne ke baad.

 package.json is one of the most imp thing for your project.


 If you have deleted the node modules of project you can
instantly download it from package.json by using command :
npm install
 There is no need to share node modules while pushing the
project on github.
 To create a package.json for your package just type : npm init
Best practise - install package locally.

To use import first export and create a package.json file if not created
and add a type : module in it and then use import.
Express.js –

Express() function ek object return karta hai use


humne app me store kar liya. Aur wo object ke bahot
se methods hote hai. Usmese ek app.listen() hai.

app.listen() ek server banata hai specified port pe aur


listen karta hai sare incoming requests ko.

App is listening on port means This


means the server is running.

Koi bhi request bhejne se pehle


server ko chalu karna padta hai.

Yahape index.js me humne backend


ka code likha hai to file run karne se
hamara server chalu ho jata hai.

app.use() – it is the method that is used to receive all kinds of


requests. It takes two pararmeters req, res
Agar hum localhost:3000 pe jate hai to use ek
objects by default. request bheji jati hai server ko(3000 port
pe).Cannot get/ ka matlab hai ki server to chal
raha hai par server ke paas suitable response
nahi hai deneke i.e liye i.e get/method ke liye
response nahi hai server ke paas.
Whenever a http request is sent, it is in the form of text but express
converts it into an object when it is received by the server
res.send() method is used to send the response to the client.
When the response is an array or object, express converts it into a
json format.
Note – there can be only one response on a request from a single
route. But there can be no. of responses depending on the number
of routes.

app.get()
The path parameters(i.e all that comes after /: ) are treated as
variables.

Handling requests with querry strings

All the querries get stored in the request object.


EJS – Embedded javascript templates is a templating language which
lets you make html templates with js functionality

Steps to render a html reponse –


Use ejs
Make a separate views directory and create a home.ejs file
Working – now the ejs will look for the views directory in current
working directory where the server has been started. And will render
the file specified in app.get()
Note – Ejs will always look for the views directory in the dir where
the server is started. So in case if the server is started in parent
directory the file wont get rendered.so to avoid this ---------

Using interpolation syntax EJS makes html page dynamic.


Includes are like subtemplates which are used to get included in the
templated like header, footer.
Data sent through get request is in string format and visible in the url.
Data sent through post request via request body can be in any form.
So express doesn’t know what kind of data is sent so it is must to
parse the received data .
Factory Functions -
They are functions which create objects.
Note – Factory functions create objects with methods having their
own copy.
This is a disadvantage of factory functions as numerous object
creation will cause numerous copies of their own methods.
New operator –
Class –

You might also like