Search code examples
node.jsexpressserverrequestmiddleware

What is Middleware in ExpressJS?


I am a beginner in Node JS and I am trying to learn website developement with Node.js and Express. I have found this word: middleware, especially when I was documenting about callback functions and app.use() function. What is middleware? What is used for? What is the connection between app.use() function and middleware?


Solution

  • software that acts as a bridge between an operating system or database and applications, especially on a network. Google

    Middleware is software which lies between an operating system and the applications running on it. Essentially functioning as hidden translation layer, middleware enables communication and data management for distributed applications. Azure

    by following the definitions, if we simply check working code:

    // Creating a course
    app.post(‘/api/courses’, (request, response) => {
    
    });
    
    // Getting all the courses
    app.get(‘/api/courses’, (request, response) => {
    
    });
    

    if you look into this code known as middleware code in express, requesting the data from the server with request and respons back that data to the client with response variable. This is the bridge between client and the server.