Can you simply set all routing configurations in a single object? Basically what I’d like to do is have a file called routing.js that exports an object containing all of the routes. Having to use express.get() for every route is mundane.
Can you simply set all routing configurations in a single object? Basically what I’d
Share
I actually have a folder that holds all the files for my routes (I split them up by object). I also separate out my controllers into files as well. The route files then look like this:
Then in my app.js file I do this which automatically loads up all of my route files and configures the 500 and 404 routes:
If you don’t want to read the files in like this, the import part is this line:
Which allows you to use the route that was defined previously.
I do it this way to maintain control over exactly which routes are created. If you want to automate the process you can use https://github.com/visionmedia/express-resource.
This allows you to do:
In order to create automatically routes that point to the following functions in ./forum:
If you want something even simpler, you could do this…