需要多个api文件nodejs

时间:2016-08-20 14:01:41

标签: node.js

我的node.js应用程序设置在我有一个/app/api文件夹的位置,该文​​件夹有几个子文件夹和子文件:

/app/api/admin/admin_api.js

/app/api/admin/another_admin_api.js

/app/api/general/generic_api.js

/app/api/general/another_generic_api.js

...

有没有办法告诉我的应用使用&需要/app/api中的所有.js文件?

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以根据自己的要求使用Browsing.js或https://www.npmjs.com/package/bundle-up2。 虽然为了便于阅读,如果您正在使用nodejs的API端,我建议不要捆绑。如果您正在使用客户端脚本,Browsify应该符合您的要求。

答案 1 :(得分:1)

第一个解决方案是在index.js中创建/app/api并从中导出模块。

例如 /app/api/index.js

const file1 = require('./file1.js');
const file2 = require('./file2.js');

module.exports = {
  file1: file1,
  file2: file2
};

现在,当您需要文件夹/app/apiconst api = require('./app/api'))时,您的模块将通过api提供。

您还可以使用globrequire-all等内容。

相关问题