简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM