forked from kuldeeprathore05/DeltaWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49Middleware.js
108 lines (87 loc) · 3.42 KB
/
49Middleware.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//01 What are Middlewares
//It is an intermediary
//In Express -- Middlewares in Express are the functions that come into play after thr server recieves the request and before response is sent to the client.
//Common milddleware function:
//-methodOverride
//-bodyParser
//-express.static //app.use(express.static(path.join(__dirname,"/public")))
//-express.urlenoded //app.use(express.urlencolded({extended:true}))
//02 Our 1st Middleware
//Middleware functions can perform the following tasks:
//-Exeute any code
//-Make changes to the request and the response objects.
//-end the request-response cycle
//-call the next middleware function in task
//1st Middleware
//app.use(middleware)
//app.use(()=>{
// console.log(("Hi i am middleware"));
// })
//using req & res object in middleware
//app.use((req,res)=>{
// console.log(("Hi i am middleware"));
// resizeBy.send("bye")
// })
//Middleware agar response send kr raha hai to agge wale kuch nhi kr paenge
//Middleware ya to response bej skta hai ya next middleware function ko call kr skta hai
//Bhot se third party middleware bhi hote hai like morgon ,passport , helmet , cooki parser ... inko install krna pdta hai
//03 Using next()
//The next middleware function is commonly denoted by a variable named next.
//app.use((req,res,next)=>{
// console.log("First MIddleware");
// next();
// })
//If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function.
//Agr next ke just badd like console.log("abc") essa likh diya to vo bhi print hoga console mein
// But if we write return next() too fir return next ke badd ka print nhi hota
//app.use((req,res,next)=>{
// console.log("First MIddleware");
// return next();
// console.log("abc")
// })
//04 Creating Utility Middlewares
//Logger -- Usefull info about req ko print karaenga(Third party middleware Morgon bhi logger jesa hi kaam krta hai)
//app.use((req,res,next)=>{
// req.responseTime= new Date(Date.now()).toString();
// console.log(req.method , req.path , req.responseTime ,req.hostname);
// next();
// })
//ye req object mein se method path hostname sab print kara dega ... Aur Ye upar date print karane ka tarika hai
//05 Exploring app.use()
//app.use("/random",(req,res)=>{
// console.log("I am only for random")
// next();
// })
//ye middleware jab hi work krega jab /ramdon pe route krenge
//06 API Token As Query String
//Lets create a middle-war for an api that checks if the excess token was passed in the query string or not.
app.use("/api",(req,res,next)=>{
let {token}=req.query;
if(token==="giveaccess"){
next();
}
else{
res.send("ACCESS DENIED")
}
})
app.get("/api",(req,res)=>{
res.send("data");
});
//esse kr skte hai jese koi chiz ko secure krna hai to middleawre laga to
// localhost8080/api //agar koi ye route krega to access deined milegi
// localhost8080/api?q=giveaccess //ye likega to data mil jega
//07 Passing multiple Middlewares
//const checkToken = (req,res,next)=>{
// let {token}=req.query;
// if(token==="giveaccess"){
// next();
// }
// else{
// res.send("ACCESS DENIED")
// }
// }
// app.get("/api",checkToken,(req,res)=>{
// res.send("data");
// })
//iss tarh apan middleware ko multiple jagh use kr skte hai ,, const krke /api route krne pe middleware ayega bech me
//08 Handling Errors(Express Default)