how to write router
why we to use router
now we runing one file
but in live we need more apis
in taht we request router taht will reported to required controller and taht response share with client
like mvc structure
model
view
controller
upto now taht middle ware is called application middle ware
to know about router create one folder is controller
in that userconroller.js
then
index.js
node index.js
go to post man
http://localhost:3000/user/details
i will explain code
go to usercontroler.js
ist import
express and router
in previously we written app.get
like that
write router.get in that req and res
write json
taht will print using res.send
if we wanna use any where that fole
export that file
got to index.js
in taht also
impoirt express
and write taht to one varaible
port is normally
to print data in json
use taht
if we wanna use user couintropller import that
imported
then use with app
whenever api requested with user
http://localhost:3000/user
so we reached index js, it wil go to user controller
if uss user/details it will get user details function from user controler
it will show json what we given in taht list
like wise write in role
create role controller
import that in our index.js file
upto now we seen
how to write show in console
how write from cotnroler
how to write directly
then we will check with mongo db connection
No comments:
Post a Comment