Reputation: 11
I'm learning Express.js and I came across this definition:
"An Express router provides a subset of Express methods. To create an instance of one, we invoke the .Router() method on the top-level Express import. To use a router, we mount it at a certain path using app.use() and pass in the router as the second argument."
What does "mount" mean?
Thank you in advance!
Upvotes: 1
Views: 476
Reputation: 114004
Mount simply means it prepends the path to your routes.
Say for example your routes have the following endpoints:
/list
/create
/update
/get/:id
And you mount it to /api/tasks
. The endpoints will be:
/api/tasks/list
/api/tasks/create
/api/tasks/update
/api/tasks/get/:id
The way it's done in Express is:
const routes = require('./my/routes');
app.use('/api/tasks',routes);
Note: This terminology comes from how disks/drives work on Unix (Linux, MacOS etc.). Unlike Windows where disks are mounted to letters like
C:
orD:
on Unix disks are mounted to folders. So for example if you connect a thumb drive named "Stuff" on Linux you will find a new folder called:/media/your_username/Stuff
And this folder will contain all the data of the thumbdrive. This is done by the automounter but you can also manually mount drives to empty folders. For example if I install the right software on my machine I can mount a remote shared drive to
/Users/my_username/projects/shared_drives/shared_pictures
.So it's natural for programmers to call the act of adding things to paths mounting.
Upvotes: 1