MEAN.js如何自动加载路由?
How does MEAN.js automatically load routes?
我正在阅读 Mean.JS docs, and regarding routes, it says that to create routes just create a .js
file similar to one of the pre-defined route files and Express will automatically use it。
魔法是如何发生的?
我发现在 config\express.js
文件中,有这个部分:
// Globbing routing files
config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
require(path.resolve(routePath))(app);
});
将 glob
路径(即 './app/routes/**/*.js'
)传递给 getGlobbedFiles()
函数,然后对返回的每个文件调用一个函数(即 foreach
)数组。返回的每个文件都是 routes
文件夹中的路由文件之一。
所有路由器因此 require
d 到应用程序中。
我正在阅读 Mean.JS docs, and regarding routes, it says that to create routes just create a .js
file similar to one of the pre-defined route files and Express will automatically use it。
魔法是如何发生的?
我发现在 config\express.js
文件中,有这个部分:
// Globbing routing files
config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
require(path.resolve(routePath))(app);
});
将 glob
路径(即 './app/routes/**/*.js'
)传递给 getGlobbedFiles()
函数,然后对返回的每个文件调用一个函数(即 foreach
)数组。返回的每个文件都是 routes
文件夹中的路由文件之一。
所有路由器因此 require
d 到应用程序中。