A
A
Andrew2019-10-19 21:05:57
JavaScript
Andrew, 2019-10-19 21:05:57

How to import one module and immediately export all methods from it?

The bottom line is this
There is a file, let's say module.js

module.exports = {
    async method1(req, res) { // code }
    method2(req, res) { // code }
}

There is controller.js
const module = require('module')

module.exports = {
  // вот здесь хочу экспортировать всё из модуля
}

To be used elsewhere
const controller = require('controller')

router.get('/', controller.method1)

The task is quite standard, but so far it has not been possible to do this.
There was an idea to make destructuring
const { method1, method 2 } = require('module')

module.exports = {
  method1,
  method2
}

I have not yet checked whether it will work at all in a node, but the disadvantages are obvious - when adding a new method to a module, you will have to register it in the controller, which is terribly inconvenient.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-10-19
@AndrewRusinas

const m = require('module')

module.exports = {
  ...m, 
  // ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question