I made a longer example of one way to do it and posted it on Github: https://github.com/j127/express_structure
The app.js
(same idea as server.js
above) file imports the routes on these lines (the way that flcpge mentioned).
Then the routers get mounted here at three different paths:
/
/things
/api
Then the router mounts controller functions on the routes. Example, the thing router sends requests for localhost:3000/things
to this function in the thingsController.js
file.
Another example:
- The app mounts the page router here.
- The page router says that when the user requests
/about
to run this function in the page controller
I used the words “page”, “thing”, and “api” in the example code — you could do similar things with any content type (“page”, “article”, “food”, etc.).