我有一个 Node Express 路由文件。它的与路由无关的函数数量变得越来越大 - 用于访问 Azure 和其他文件名相关函数的代码。
我相当确定我想在路由文件中保留路由函数(这里是 route/tally.js
文件,以及另一个文件中的文件名解析函数(即 fileName.js
和 tallyAzure.js
)。
如何告诉我的route.js 文件包含并引用另一个.js 文件中的所有函数。
对于路由 js 文件,我尝试了
include("routes/tallyAzure.js");
。解决方案中放置这些函数文件最合理的位置在哪里?我倾向于使用
routes
目录之外的目录。我应该注意哪些命名约定?
最佳答案
您需要使用exports
变量。以下是文档中的示例:http://nodejs.org/api/modules.html
circle.js:
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
app.js:
var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
+ circle.area(4));
关于node.js - 在 Node Express 中的 js 文件中包含并分组相关函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24620839/