从单独的路径文件中访问快速“app”对象的最佳方法是什么? [英] What's the best way to access the express 'app' object from inside a separate route file?
本文介绍了从单独的路径文件中访问快速“app”对象的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Express 4中,默认情况下,路线是从单独的文件加载的:
In Express 4, by default, routes are loaded from a separate file:
app.use('/', routes);
将加载 routes / index.js
。
我有一个附加到 app
本身的第三方库。是否有一种首选方法可以从 routes / index.js
中访问 app
?
I have a third-party library that attaches to app
itself. Is there a preferred way to access app
from inside routes/index.js
?
我考虑过依赖注入,即 routes / index.js
确实
I've thought about dependency injection ie, routes/index.js
does
module.exports = function(app){
(routes go here)
}
然后:
app.use('/', routes(app))
但我想知道是否有更好的方法。 从单独的路径文件中访问快递'app'对象的最佳方法是什么?
But I wonder if there's a better way. What's the best way to access the express 'app' object from inside a separate route file?
推荐答案
您只需通过路线处理程序中的req.app访问应用程序
You can simply access app by req.app in your route handlers
这篇关于从单独的路径文件中访问快速“app”对象的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文