How it works...
The /routes/api.js file is a simple vanilla Node.js module that we import into our main Express /app.js file using the require command. This file's contents could have been put into the same place as the rest of our server's configuration. However, it's best practice in Express to separate middleware and routing layers from top-level application configuration, for the purpose of better modularization.
Keeping these layers separate in this way also makes it easier to make changes to your web server's routing at the top level, if needed. For instance, we defined all our API routes as relative routes in our /routes/api.js file, but namespaced them to /api in our app.js file. If we suddenly had a reason to change this namespace, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access