Skip to content

Commit

Permalink
creating middleware in same file
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 4, 2022
1 parent 230ca36 commit 8bad0e3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const express = require('express');
const app = express();


//creating a middleware
const reqFilters = (req , res , next) => {
if(!req.query.age){
res.send('<h1>Please Provide Age</h1>');
}

else if(req.query.age < 18){
res.send('<h1>You can not access this page</h1>');
}

else{
next();
}
}

//Using Middleware
app.use(reqFilters);

app.get('/' , (req,res) => {
res.send('<h1>Welcome to home page</h1>');
});

app.get('/user' , (req , res) => {
res.send('<h1>Welcome to users page</h1>')
})

app.listen(8000 , (err) => {
if(!err){
console.log('Server is running on the port::8000');
}
});

0 comments on commit 8bad0e3

Please sign in to comment.