-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteParams.js
42 lines (31 loc) · 1.52 KB
/
routeParams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const express= require('express');
const app= express();
const {products}= require('./testFiles/data.js');
app.get('/',(req,res)=>{
res.send("<h1>Home Page</h2><a href='/api/products'>Our Products</a>")
})
app.get('/api/products',(req,res)=>{
const newProducts=products.map((ele)=>{
const{id,name,image}=ele;
return{id,name,image};
})
res.json(newProducts); //json e convert kore pathabe newProducts ta
})
/*Kono ekta specific product ber korte chaichi, setar jonno route parameter use korbo */
app.get('/api/products/:productID',(req,res)=>{ // : diye route params likhte hoy
console.log(req.params); //{productID:'2'} for the req.url /api/products/2 . i.e. products/ diye jeta likhbo seta productID key er value hoye req.params obj te store hobe.
const {productID}=req.params;
const singleProduct=products.find((ele)=>{
return ele.id==Number(productID);
})
if(!singleProduct) return res.status(404).send('Product Does Not Exist'); //return ta dite hobe otherwise if block ta execute hoar poreo next res.send ta execute hoye jabe, ekta req er jonno duto response pathano jayna
res.json(singleProduct);
})
/*Complex route params */
app.get('/api/products/:productID/reviews/:reviewID',(req,res)=>{ // : diye jegulo lekha ache segulo route params hisebe kaj korbe
console.log(req.params); //{ productID: 'abc', reviewID: 'xyz' } for req.url /api/products/abc/reviews/xyz
res.send('Hello World');
})
app.listen(80,()=>{
console.log('The server is listening on port 80');
})