How NodeJS middlewares works?

  express, javascript, middleware, node.js

My question is why console.log("after middleware") is printing only once while app.use(foo) is calling after every page reload. Does using NodeJS code is interpreted one time after use npm start in console despite page reload?

const express = require('express')
const app = express()

function foo(req, res, next) {
    console.log("middleware1")
    next()
}

app.use(foo)
console.log("after middleware");

app.get('/', function(req,res){
    res.send("main page")
})

app.listen(3000)

Source: Ask Javascript Questions

LEAVE A COMMENT