middleware function code example
Example 1: middleware
"middleware" - code that runs before the final route call back.
They are in the middle of the beginning of the route and the
callback function.
Example 2: what is middleware in node js
Notice 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.
Example 3: middleware in node js
var express = require('express')
var app = express()
var myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
app.use(myLogger)
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000)
Example 4: middleware
app.use("/", (req, res, next) => {
console.log("I am a middleaware")
next()
})
Example 5: what is app.use() used for
app.use( "/book" , middleware);
app.all( "/book" , handler);
app.all( "/book/*" , handler);
Example 6: middleware
, PostController::class)->middleware('auth:api');