Hasani
Hasani

Reputation: 3919

Why don't we use "express.use" in NodeJS applications?

I have seen something like this in a NodeJS application:

const express = require('express');
const app = express();
app.use(bodyParser.json());

Why didn't it use express like below:

const express = require('express');
express.use(bodyParser.json());

Upvotes: 2

Views: 61

Answers (1)

Brad
Brad

Reputation: 163538

When we call require('express'), we're essentially loading the module so that we can use it.

Express is set up in a way that its default export is a function that when called returns a fresh instance of Express.

Some applications may want to use multiple instances, which is why we wouldn't use express.use().

Upvotes: 4

Related Questions