Recently, I came across a website that only renders EJS templates using Express.js routing. All the routes are defined in the /routes/index.js
file. As the file grows with more routes being added, I am thinking about restructuring it to make it more developer-friendly. Despite having multiple categories, the URLs are only one level deep.
Check out this navigation bar which displays categories and pages.
For instance, when you click on "Gamble Diagram," it redirects you to the route /acidbase-belance
. A more logical approach would be /acidbase/belance
to avoid code repetition.
Current setup:
<!-- Current route implementations -->
Considering the code repetition and the need for a better structure, I have a few ideas in mind:
Split each category into a separate file (
routes\categorieName.js
).Create an array of possible routes in
routes\index.js
and implement a lookup mechanism.
<!-- Example lookup mechanism -->
Alternatively, a combination of both ideas by splitting categories into separate files and implementing a mechanism for pages.