[英]What's the difference between application- and router level- middleware when routing in Express?
In the Express docs , it says:在Express docs 中,它说:
Application level middleware are bound to an instance of express, using app.use() and app.VERB().
应用级中间件使用 app.use() 和 app.VERB() 绑定到 express 的实例。
Router level middleware work just like application level middleware except they are bound to an instance of
express.Router()
.路由器级中间件就像应用级中间件一样工作,除了它们绑定到
express.Router()
的实例。 The middleware system created at the application level in the example above, can be replicated at the router level using the following code.在上例中在应用程序级别创建的中间件系统可以使用以下代码在路由器级别进行复制。
In the app provided by the Express generator, I see in the main app.js
, there is:在 Express 生成器提供的应用程序中,我在主
app.js
看到,有:
var routes = require('./routes/index');
app.use('/', routes);
And in ./routes/index.js
, I see this:在
./routes/index.js
,我看到了这个:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
What is the purpose of app.use
passing it to router.get
instead of simply using app.get
? app.use
将它传递给router.get
而不是简单地使用app.get
的目的是什么? And generally, what's the difference between app.VERB
and router.VERB
in terms of routing?一般来说,
app.VERB
和router.VERB
在路由方面有什么区别?
What is the purpose of app.use passing it to router.get instead of simply using app.get?
app.use 将它传递给 router.get 而不是简单地使用 app.get 的目的是什么?
This is all just designing for modularity.这一切都只是为模块化而设计。 It allows an application to be divided into several smaller loosely-coupled pieces and because they all do not have any direct knowledge of the shared
app
instance that combines them together, the modularity is achieved.它允许将应用程序分成几个较小的松散耦合部分,并且由于它们都没有将它们组合在一起的共享
app
实例的任何直接知识,因此实现了模块化。
For example, you could build a whole user account subsystem that handles sign up, login, forgot password, etc, and share it between several different applications by "mounting" it within them via app.use(require("my-user-system"))
.例如,您可以构建一个完整的用户帐户子系统来处理注册、登录、忘记密码等,并通过
app.use(require("my-user-system"))
。
That's the only purpose behind this.这是这背后的唯一目的。 There no functional, technical, or performance difference otherwise.
其他方面没有功能、技术或性能差异。
And generally, what's the difference between app.VERB and router.VERB in terms of routing?
一般来说,app.VERB 和 router.VERB 在路由方面有什么区别?
No difference.没有不同。 The app has it's own main/primary router and
app.VERB
is just convenience sugar for the equivalent of app.router.VERB
.该应用程序有自己的主/主路由器,而
app.VERB
只是相当于app.router.VERB
便利糖。
An example will help here:一个例子将在这里有所帮助:
In the birds.js:在birds.js 中:
// birds.js
var express = require('express');
var router = express.Router();
// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page');
});
In the app.js:在 app.js 中:
// app.js
const express = require('express')
const app = express()
var birds = require('./birds');
app.use('/birds', birds);
app.listen(3000, () => console.log('app listening on port 3000!\naccess http://localhost:3000/birds/'))
Now requests to http://localhost:3000/birds/ will go to birds.js file.现在对http://localhost:3000/birds/ 的请求将转到 Birds.js 文件。
This modular approach will make easy to maintain a large code base, as the application grows big.随着应用程序变大,这种模块化方法将使维护大型代码库变得容易。
Source: https://expressjs.com/en/guide/routing.html来源: https : //expressjs.com/en/guide/routing.html
Application has the method listen and some other which router doesn't have.应用程序具有侦听方法和路由器没有的其他方法。
Application "does" the main thing, while route only groups some routes, provides encapsulation (see Manohar's answer with example).应用程序“做”主要的事情,而路由只对一些路由进行分组,提供封装(参见 Manohar's answer with example)。
Routes in router doesn't necessarily know in advance what will be its full route.路由器中的路由不一定事先知道它的完整路由是什么。 See in the example that '/birds' is defined in app.js which is root for router in birds.js ... difference between app.VERB and rourer.VERB is in the context.
在示例中,'/birds' 是在 app.js 中定义的,它是 Birds.js 中路由器的根...... app.VERB 和 rourer.VERB 之间的区别在于上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.