user2257348
user2257348

Reputation: 11

Is the express app object also an express middleware?

I just started learning express and I read that an express middleware is a Javascript function to handle HTTP requests. It accepts 3 parameters:- req, res and next.

When I tried the following code block:-

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

var x = app.get('/', function(req, res){
    res.send('Hello World!');
} );

console.log(x.toString());

app.listen(3000, function() {
    console.log('Example app listening on port 3000!');
} );

I see the following output:-

function(req, res, next){
    app.handle(req, res, next);
}

So is the express app object also an express middleware?

I'm sorry if this is a silly question, but wanted to have an insight nevertheless.

Upvotes: 0

Views: 62

Answers (1)

Prodigle
Prodigle

Reputation: 1797

The .get() function is yes. the majority of the express functions .use() etc are really just middlewares for specific tasks. .get checks the req object against the url provided, if it's a match then it runs it's code. If it isn't then it continues to next() through all the other .get() middlewares.

The entire ecosystem of express is middlewares going to more middlewares

Upvotes: 0

Related Questions