1var app = require("express")();
2
3//This is the middleware function which will be called before any routes get hit which are defined after this point, i.e. in your index.js
4app.use(function (req, res, next) {
5
6 var authorised = false;
7 //Here you would check for the user being authenticated
8
9 //Unsure how you're actually checking this, so some psuedo code below
10 if (authorised) {
11 //Stop the user progressing any further
12 return res.status(403).send("Unauthorised!");
13 }
14 else {
15 //Carry on with the request chain
16 next();
17 }
18});
19
20//Define/include your controllers
21
1//app.get will see only exact match ex.> "/book" here app.get will not allow /book/1, etc
2//but app.use is different see below
3
4//what is difference between app.use and app.all
5//app.use takes only 1 callback while app.all takes multiple callbacks
6//app.use will only see whether url starts with specified path But, app.all() will match the complete path
7
8app.use( "/book" , middleware);
9// will match /book
10// will match /book/author
11// will match /book/subject
12
13app.all( "/book" , handler);
14// will match /book
15// won't match /book/author
16// won't match /book/subject
17
18app.all( "/book/*" , handler);
19// won't match /book
20// will match /book/author
21// will match /book/subject