node js api routing | node js tutorials

 

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



const express = require('express');
const app = express();
var user_routing= require("./controller/usercontroller")
const PORT=3000;
app.use(express.json());

app.use("/user",user_routing);
app.listen(PORT,() =>
{
console.log("server on::"+PORT)
})


then 

index.js

const express = require('express');
const app = express();
var user_routing= require("./controller/usercontroller")
const PORT=3000;
app.use(express.json());

app.use("/user",user_routing);
app.listen(PORT,() =>
{
console.log("server on::"+PORT)
})

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 

var express = require('express');

const router = express.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

router.get('/details',(req,res,next) => {
let person_list=[
{name:"huderee",addr:"vamsi1",id:1},
{name:"hudere",addr:"vamsi2",id:2},
{name:"hudere",addr:"vamsi3",id:3}
];
res.send(person_list);
});


if we wanna use any where that fole 

export that file

module.exports = router;

got to index.js


in taht also 

impoirt express

and write taht to one varaible

const express = require('express');
const app = express();


port is normally

const PORT=3000;
app.listen(PORT,() =>
{
console.log("server on::"+PORT)
})

to print data in json 

app.use(express.json());

use taht

if we wanna use user couintropller import that

var user_routing= require("./controller/usercontroller")

imported 

then use with app

app.use("/user",user_routing);


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


router.get('/details 


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


var role_routing= require("./controller/rolecontroller")
app.use("/role",role_routing);

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





U CAN MAKE WITH NODEJS
WEBRTC SERVER
TCP IP SERVER

MODEL - it works with database
in database - it will store in particular Format
that format will be define by model

when u use async u have to use await

No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...