我们为什么不使用"express.use"?在NodeJS应用程序中? [英] Why don't we use "express.use" in NodeJS applications?

查看:100
本文介绍了我们为什么不使用"express.use"?在NodeJS应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NodeJS应用程序中看到了类似的内容:

I have seen something like this in a NodeJS application:

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

为什么不像下面这样使用express:

Why didn't it use express like below:

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

推荐答案

调用require('express')时,实际上是在加载模块以便可以使用它.

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

Express的设置方式是其默认导出是一个函数,当调用该函数时,它会返回Express的新实例.

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

某些应用程序可能要使用多个实例,这就是为什么我们不使用express.use()的原因.

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

这篇关于我们为什么不使用"express.use"?在NodeJS应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆