[Tutorial] An Introduction to Node.js Servers (and Express.js)

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:

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.).

1 Like