Create Modularity - bommezijn/Dating-Shreks GitHub Wiki

This topic was worked on by Nathan.

You are creating cleaner routing files. In the best-case scenario and modularity, you don't want to write the logic in the routes. You want to have the route separate from the logic.

//This
router.get('/', urlencodedParser, getHome);

//Instead of this
router.get('/', urlencodedParser, (req, res) => {
  function home(req, res) {
      if (!req.session.nameID) {
        res.render('/');
      } else {
        res.redirect('succes');
      };
    };
})

Not only will it readable when you are going to do unit testing with, for example, Mocha, Jest, Yeoman, Webpack, or other unit testing software. But besides testing, clean routing is crucial for the present but also the feature.

Your application is scaling, more links, more pages more dynamic routing is required, If you keep it all in a single file, no one will be happy, neither will the codebase be satisfied when you add to said file, while your team also work on that file. It will become a big git merge conflict that will require hours of debugging and checking.

Splitting route logic allows for clean routes, extensible logic files, and easier reuse. But setting it up isn't easy either. Most of the examples on the internet also subconsciously do this with the MVC model. a Model-View-Controller-model is a design pattern that helps the team design complex applications, while working together, creating clear logic and precise routes.

The downside of those design patterns (Splitting logic and MVC) is that navigating will become complicated, and MVC can be negligible by separating UI applications into components, thus already achieving code reuse.


Sources

Express basic routing. (n.d.). ExpressJS. Retrieved June 23, 2020, from http://expressjs.com/en/starter/basic-routing.html

Express Tutorial Part 4: Routes and controllers. (2020, May 18). MDN Web Docs. https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes

Sears, A. (2016, March 31). Keeping API Routing Clean Using Express Routers. Scotch.Io. https://scotch.io/tutorials/keeping-api-routing-clean-using-express-routers