Skip to content

Commit

Permalink
creating seperate file for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 8, 2022
1 parent 089a5c1 commit af2e7c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions middleware4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const reqFilter = require('./middlewareFile');
const app = express();

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


//appliying middleware on single route
app.get('/user' , reqFilters , (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');
}
});
13 changes: 13 additions & 0 deletions middlewareFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = 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();
}
}

0 comments on commit af2e7c9

Please sign in to comment.