gpt4 book ai didi

node.js - 需要多个nodejs api文件

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:22 25 4
gpt4 key购买 nike

我的 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 文件?

提前致谢!

最佳答案

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

例如 /app/api/index.js:

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

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

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

您还可以使用类似 glob 的内容或require-all .

关于node.js - 需要多个nodejs api文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39054974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com