-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlew3.js
43 lines (35 loc) · 921 Bytes
/
middlew3.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
const express = require("express");
const app = express();
//a middleware function act as a checker
// function isUserOldEnough(age) {
// if (age >= 18) {
// return true;
// } else {
// return false;
// }
// }
function isUserOldEnoughMidWare(req, res, next){
const age = req.query.age;
if (age >= 18) {
next();
} else {
res.json({
status: "fail",
message: "you are not old enough"
});
}
}
//or just write one line here and it will used by the routes
// app.use(isUserOldEnoughMidWare)
//app.use gets triggered below it for all the routes , it must be above all the routes you want to use it for
app.get("/ride1", isUserOldEnoughMidWare ,function (req, res) {
res.json({
message: "Ridden the ride 1",
});
});
app.get("/ride2", isUserOldEnoughMidWare ,function (req, res) {
res.json({
message: "Ridden the ride 2",
});
});
app.listen(3000);