Codecademy Forums

Are there naming conventions on router files?


Are there naming conventions on router files?


Nothing required, like many other things in Express, route files are flexible, but in terms of community, there has been a certain agreement on naming conventions, to pair the file name with the route, and some places do ask to write the word route as part of the name. ie monsterRoutes.js but again, most commonly we can see simply monsters.js related to the /monsters route. Another thing to have in mind is that having a router.js file that handles all the route connections is also common, for example the following file map:

__ app.js
__ router.js
__ routes/
         __ monsters.js
         __ animals.js
         __ fruits.js

We can see that now we could also have a directory of routes where each route handler file exists and are connected to our app through a main router.js file. This sort of system is commonly seen when there is a hight complexity in our routes that is better to have a middleware to handle routes instead of implementing app.use('/route', routeHandler) inside of app.


Could you give us an example code of router.js? Would it be something like:

const express = require('express');
const router = express.Router();
const monsters = require('./routes/monsters.js');
const animals = require('./routes/animals.js');
const fruits = require('./routes/fruits.js');

router.use('/routes', monsters, animals, fruits);

module.exports = router;

and then in app.js,

const router = require('./router.js');
app.use('/', router);

Am I correct?