Building Custom Middleware Function

Building Custom Middleware Function

Note: our middleware functions are called in sequence.

app.use()

we call this method to Install a middleware function in our request processing pipeline


next();

It is a reference to the next middleware function in the pipeline To pass control to the next middleware function in the pipeline


//app.js
// our middleware function are called in sequence.
const logger = require("./logger");
const express = require("express");
const app = express();

//here we adding a json middleware function
app.use(express.json());

//importing Custom Middleware Function from outside
app.use(logger);

//Building Custom Middleware Function
app.use(function (req, res, next) {
  console.log("Logging...");
  next();
});
//logger.js

//Building Custom Middleware Function

function log(req, res, next) {
  console.log("Authenticating...");
  next();
}

module.exports = log;