1var express = require('express')
2var app = express()
3
4var myLogger = function (req, res, next) {
5 console.log('LOGGED')
6 next()
7}
8
9app.use(myLogger)
10
11app.get('/', function (req, res) {
12 res.send('Hello World!')
13})
14
15app.listen(3000)
16
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
1Notice the call above to next(). Calling this function invokes the next middleware function in the app. The next() function is not a part of the Node.js or Express API, but is the third argument that is passed to the middleware function. The next() function could be named anything, but by convention it is always named “next”. To avoid confusion, always use this convention.
2