问题
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());
回答1:
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().
来源:https://stackoverflow.com/questions/59512833/why-dont-we-use-express-use-in-nodejs-applications