Do we always keep the routers in app.js?


Do we always keep the routers in app.js?


Not always, it is not a necessary pattern since it would usually depend on how your team likes to structure files, for example, other teams like to set up a router handler file that simply contains all the router files and exports them:


const router = require('express').Router();

router.get('/', (req,res) => res.render('index'));
router.use ('/expressions', require(''./expressions.js''));
router.use('/animals', require('./animals.js'));

module.exports = router;

This allows for a more simplistic set up on our app.js (or also called index.js )


const express = require('express');
const app = express();

const PORT = process.env.PORT || 4001;
// Use static server to serve the Express Yourself Website

//requiring our router to be connected to app

app.listen(PORT, () => {
  console.log(`Server is listening on ${PORT}`);

This way app is only taking care of the connection set up and we leave the handling of routers to the router.js file.

Thankfully Express is a very mix-and-match kind of library so we can also create our own structure system, this way is also something that works specifically for ourselves, a drawback of it is that there are no real standards so when it comes to errors and how to better structure our files, everyone will have their opinion, so let’s also explore what works best for us

1 Like

Where can I find router.js file?