InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

Inversify-express-utils: Dynamic Loading and registering controllers at runtimes after starting the server instance

Open SaifJerbi opened this issue 7 years ago • 3 comments

This issues is related to inversify-express-utils

Dynamic Loading and registering controllers at runtimes after starting the server instance

Expected Behavior

I'm developing a modular runtime application with nodeJs. I used inversify to implement the required IoC mechanism. It helps me to decouple my modules and handle the Dependency Injection between modules. My requirement today is to load a module at runtime. The scenario is :

  • The administrator install a new module (node module) at runtime
  • The module contains some controllers and DI classes ( I'm using inversify-express-utils to define controllers)
  • The core module (main app) load this module and register the new controllers => So new Rest Paths comes available.

What i can do :

  • In the core module: i got to deal with registring the DI classes into the inversify container. It works :clap:
  • Read the new controllers from new module :clap:

What i can't do / I didn't find how it should be done:

  • Register the new controllers (that comes with the new installed module) and updates the app routes.

N.B: registering a new routes at runtime is provided by express app.

Is there anyone of you dears, who can help me? Do you find this requirement useful to be integrated in the Inversify-express-util ? I can do a pull request i have made some changes to deal with this issue.

Current Behavior

The current implementation doesn't provide the possibility to update the routes of the running instance. All controllers must be loaded and registered at start time

Possible Solution

Work in progress to implement the cleaner solution in order to provide a new API function that allow developer to load dynamic controllers / fetch the new controllers via Reflect Meta Data / Register only the new paths to routes

Context

Developing modular runtime application : In a modular environment, modules are loaded and unloaded at runtime (also installed) basic on end-user manipulation (ex: Administrator). So there will be new controllers (paths) available at runtime. Adding the possibility to register routes (controllers) at runtime is very useful for this kind of projects.

SaifJerbi avatar Apr 27 '18 08:04 SaifJerbi

Any update on this??

johndoe2361289638163 avatar Dec 23 '18 14:12 johndoe2361289638163

I know that this issue is very very oldest, but i think that i can help any person.

I've importing dynamically controllers using this code, but just before the server build

const path = require('path');

const loadHandlers = () => {
  return new Promise(async (resolve, reject) => {
    const fs = require('fs');
    const dir = path.join(process.cwd(), process.env.NODE_ENV != 'production' ? 'src' : '','/Presentation/Handlers')
    log(LogLevelEnum.INFO, `Loading controllers from ${dir}`)

    await fs.readdir(dir, async (err, files) => {
      let reducers = files.filter(a => a.includes('.'));
      let references = [];

      reducers.forEach(reducer => {
        references.push(reducer);
      });
      await references.map(async (ref) => {
        console.log(path.join(dir, ref))
        await import(path.join(dir, ref))
      })
      console.log(Object.keys(require('module')._cache).filter(a => !a.includes('node_modules')))

      resolve(true)

    });
  })
}

after you just need call before the server build

(async () => {
  await loadHandlers()
    
  server.build().listen(port);
})();

You can implement server built be hot reload , but i didnt tested

ceelsoin avatar Dec 31 '21 17:12 ceelsoin

The same problem.

Thanks for help.

RosarioDeveloper avatar Aug 03 '23 13:08 RosarioDeveloper