Node Js
Node Js
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.
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.
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.
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.
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 –
app.get()
The path parameters(i.e all that comes after /: ) are treated as
variables.