node.js Express 中 app.use() 和 router.use() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27227650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference Between app.use() and router.use() in Express
提问by Naeem Shaikh
I was just reading the documentation on express and found these two terms, app.use();and router.use();
我只是在阅读有关 express 的文档并找到了这两个术语,app.use();并且router.use();
I know app.use();is used in node for Mounting a middlewareat a path, and we often use it in most of the node apps. but what is router.use();are they both same? if not, whats the difference ?
我知道app.use();在节点中用于在路径上安装中间件,我们经常在大多数节点应用程序中使用它。但router.use();它们有什么相同之处?如果没有,有什么区别?
I read about router here. I also found similar questions on SO What is the difference between "express.Router" and routing using "app.get"?and Difference between app.all('*') and app.use('/'), but they do not really answer my question. Thanks.
我在这里阅读了有关路由器的信息。我也在 SO 上发现了类似的问题“express.Router”和使用“app.get”的路由有什么区别?和app.all('*') 和 app.use('/') 之间的区别,但它们并没有真正回答我的问题。谢谢。
回答by Sudhanshu Gaur
router.getis only for defining subpaths. Consider this example:
router.get仅用于定义子路径。考虑这个例子:
var router = express.Router();
app.use('/first', router); // Mount the router as middleware at path /first
router.get('/sud', smaller);
router.get('/user', bigger);
- If you open /first/sud, then the
smallerfunction will get called. - If you open /first/user, then the
biggerfunction will get called.
- 如果您打开/first/sud,则该
smaller函数将被调用。 - 如果您打开/first/user,则该
bigger函数将被调用。
In short, app.use('/first', router)mounts the middleware at path /first, then router.getsets the subpath accordingly.
简而言之,app.use('/first', router)在路径/first安装中间件,然后相应地router.get设置子路径。
But if we instead use the following:
但是,如果我们改为使用以下内容:
app.use('/first', fun);
app.get('/sud', bigger);
app.get('/user', smaller);
- If you open /firstin your browser,
funwill get called, - For /sud,
biggerwill get called - For /user,
smallerwill get called
- 如果您在浏览器中打开/first,
fun将被调用, - 对于/sud,
bigger将被调用 - 对于/user,
smaller将被调用
But remember for /first/sud, no function will get called.
但请记住/first/sud,不会调用任何函数。
This link may also help: http://expressjs.com/api.html#router
此链接也可能有帮助:http: //expressjs.com/api.html#router
回答by Laura
router.use();mounts middleware for the routes served by the specific router, app.use();mounts middleware for all routes of the app (or those matching the routes specified if you use app.use('/ANYROUTESHERE', yourMiddleware());).
router.use();为特定路由器提供服务的路由app.use();挂载中间件,为应用程序的所有路由(或那些匹配指定的路由,如果使用app.use('/ANYROUTESHERE', yourMiddleware());)挂载中间件。
Example use case could be an app with one router with standard routes and one router that handles api routes, which need a valid user.
示例用例可能是一个应用程序,其中一个路由器带有标准路由,一个路由器处理 api 路由,这需要一个有效用户。
You would then mount the authentication middleware for the api router only with router.use(yourAuthMiddleware());.
然后,您只需使用router.use(yourAuthMiddleware());.
If you would have an app though that requires a valid user for all routes, mount the middleware for the app with app.use(yourAuthMiddleware());
如果您有一个应用程序,但它需要所有路由的有效用户,请为该应用程序挂载中间件 app.use(yourAuthMiddleware());
回答by Shubham Verma
app.use() used to Mounts the middleware function or functions at the specified path,the middleware function is executed when the base of the requested path matches path.
app.use() 用于在指定路径挂载一个或多个中间件函数,当请求路径的基址与路径匹配时执行中间件函数。
router.use() is used to middleware function or functions, The defaults mount path to “/”.
router.use() 用于中间件函数或函数,默认挂载路径为“/”。
But in app.use() you will have to give a specified path like that:
但是在 app.use() 中,您必须提供这样的指定路径:
var adsRouter = require('./adsRouter.js');
app.use('/ads', adsRouter);
or
或者
app.use('/ads', function(req, res, next) {
// write your callback code here.
});
But while using router.use() you can give only middleware, like this:
但是在使用 router.use() 时,您只能提供中间件,如下所示:
router.use(function(req, res, next) {
console.log('%s %s %s', req.method, req.url, req.path);
next();
});
or
或者
router.use('/test', function(req, res, next) {
// write your callback code here.
next();
});
or
或者
//in router.js
router.use('/admin', authUtil.verifySessionId, authUtil.verifyLisencee);
router.post('/admin', controllerIndex.ads.adListingAdmin);
In the above code when the end point is '/admin' then first it will call the authUtil.verifySessionId and authUtil.verifyLisencee then it will execute next line with 'admin' end point and according to controllerIndex.ads.adListingAdmin method.
在上面的代码中,当端点是“/admin”时,它首先会调用 authUtil.verifySessionId 和 authUtil.verifyLisencee,然后它会根据 controllerIndex.ads.adListingAdmin 方法执行带有“admin”端点的下一行。

