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

萝らか妹 提交于 2020-01-24 19:05:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!