-1

In ./app_api/routes/index.js

var router = express.Router()
var ctrlLocations = require('../controllers/locations')
router.post( '/', ctrlLocations.helloCreate );
module.exports = router

In app.js

var routesApi = require('./app_api/routes/index');
app.use('/', routesApi)

I am guessing that variable routesApi contains a reference to the variable router of index.js.
What does require() return which is used in app.use()?

How does app.use('/', routesApi) know which function to call from index.js. There can be many functions there? How does this work internally?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • Does this answer your question? [What is the purpose of Node.js module.exports and how do you use it?](https://stackoverflow.com/questions/5311334/what-is-the-purpose-of-node-js-module-exports-and-how-do-you-use-it) – jonrsharpe Jan 19 '21 at 12:15

1 Answers1

1

I am guessing that variable routesApi contains a reference to the variable router of index.js

It contains a copy of the value of module.exports which was a copy of the value of router.

JavaScript doesn't support references to variables.

What does require() return which is used in app.use()?

The same as it returns anywhere else. The express router object.

How does app.use('/', routesApi) know which function to call from index.js. There can be many functions there?

It does URL path matching on the paths specified as the first argument to each of the routes created on the router.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335