user1547174 user1547174 - 1 year ago 115
Node.js Question

Require multiple api files nodejs

My node.js app is set up where I have an

folder, which has several subfolders and subfiles as such:






Is there a way I can tell my app to use & require ALL of the .js files in

Thanks in advance!

Answer Source

The first solution would be to create an index.js in /app/api and export your modules from it.

e.g /app/api/index.js :

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

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

Now when you require the folder /app/api (const api = require('./app/api')) your modules will be available through api.

You also can use something like glob or require-all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download