简体   繁体   English

express.Router() 与多个 express() 对象之间的区别?

[英]Difference between express.Router() vs multiple express() object?

I know using the new express 4 router we can organize multiple route paths into different files like this:我知道使用新的 express 4 路由器我们可以将多个路由路径组织到不同的文件中,如下所示:

// In cars.js
 const router = express.Router();
 router.get('/brands', function(req, res) { ... });
 router.get('/models', function(req, res) { ... });
 module.exports = router;

// In animals.js
const routerTwo = express.Router();
routerTwo.get('/domestic', function(req, res) { ... });
routerTwo.get('/wild', function(req, res) { ... });
module.exports = routerTwo;

// In main.js
app.use('/cars', router);  // matches `/cars/brands`, `/cars/models`.
app.use('/animals', routerTwo); // matches `animals/domestic`, `animals/wild`.

Now I want to know if I change the express.Router() with simply express() in both cars.js and animals.js file, it'll work because both acts as new instance of express() which we called mini-app.现在我想知道如果我改变express.Router()用简单的express()两个cars.jsanimals.js文件,它会工作,因为这两个充当快递()我们称之为的小应用程序的新实例. Then what is the use of new express.Router() object?那么new express.Router()对象有什么用呢?

Aren't the same methods exposed in express.Router() object, with their functionality, achievable via multiple express() instance like modular router handlers etc.? express.Router()对象中公开的相同方法不是可以通过多个express()实例(如模块化路由器处理程序等express()实现的功能吗?

In simplest terms, Router is a lightweight version of the express app, or as Express docs put it , a mini express application.简单来说,Router 是 express 应用程序的轻量级版本,或者如 Express 文档所说,是一个迷你 express 应用程序。

The global express object comes with many more resources to support views and various settings while the router basically provides the routing APIs like .use , .get , .param , and route .全局 express 对象带有更多资源来支持视图和各种设置,而路由器基本上提供路由 API,如.use.get.paramroute A router object represents an isolated instance of middleware and routes and is only capable of performing middleware and routing functions, which makes it perfect for efficiently modularizing your route handling.路由器对象代表中间件和路由的隔离实例,只能执行中间件和路由功能,非常适合高效模块化路由处理。

Now I want to know if I change the express.Router() with simply express() in both cars.js and animals.js file, it'll work because both act as new instance of express() which we called mini-app.现在我想知道我是否在cars.js 和animals.js 文件中使用express()更改express.Router() ,它会起作用,因为它们都充当express() 的新实例,我们称之为mini-app . Then what is the use of new express.Router() object?那么new express.Router()对象有什么用呢?

Well, you should still be able to use express() instead of express.Router() for isolated routing purposes but with that you are also bringing much extra code that you probably won't use so it's an unnecessary overhead.好吧,您应该仍然可以使用express()而不是express.Router()用于隔离路由目的,但同时您也带来了许多您可能不会使用的额外代码,因此这是不必要的开销。

That is why the express.Router() was created: to provide route modularization without the overhead of creating an additional app.这就是创建express.Router()原因:提供路由模块化,而无需创建额外的应用程序。

Related: https://en.wikipedia.org/wiki/Liskov_substitution_principle相关: https : //en.wikipedia.org/wiki/Liskov_substitution_principle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM