Skip to content

Commit

Permalink
applying middleware by using route
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 8, 2022
1 parent af2e7c9 commit 679f65e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
32 changes: 32 additions & 0 deletions middleware5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express');
const reqFilter = require('./middlewareFile');
const app = express();
const route = express.Router();

route.use(reqFilter);

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

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

//Applying Middleware by using route
route.get('/user' , (req , res) => {
res.send('<h1>Welcome to users page</h1>')
});

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

app.use('/' , route);


app.listen(8000 , (err) => {
if(!err){
console.log('Server is running on the port::8000');
}
});
3 changes: 2 additions & 1 deletion middlewareFile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//exporting reqFilter from here here to be used by some other files
module.exports = reqFilters = (req , res , next) => {
if(!req.query.age){
res.send('<h1>Please Provide Age</h1>');
Expand All @@ -10,4 +11,4 @@ res.send('<h1>You can not access this page</h1>');
else{
next();
}
}
}

0 comments on commit 679f65e

Please sign in to comment.