Refining Node.js Architecture: Elegant Separation of Routers and Controllers

Refining Node.js Architecture: Elegant Separation of Routers and Controllers

A few years ago, I designed several portals for an educational institution in Toronto.

The portals were developed with Angular 7 for the front end and utilized a REST API with Express and Node.js on the back end. They communicated with a database and were integrated with Azure B2C Single Sign-On within the Azure Cloud.

Recently, I decided to rewrite a similar application using the latest Angular version (18) and to document every step of the design, coding, and deployment process for a book I am planning to author.

While beginning this project, I encountered a tip that I believe is worth sharing with you. This tip relates to the implementation of routes and controllers for a Node.js REST API server.

A popular method for separating routes and controllers in a Node.js REST API server is outlined in several online articles, such as the one mentioned below:

https://www.geeksforgeeks.org/how-to-separate-routers-and-controllers-in-node-js/

Import the controller class and create a route for the controller's method

Dan Wahlin presents a more elegant approach in his Pluralsight course, 'Integrating Angular 11 with Node.js RESTful Services', where routes are automatically created upon application startup and defined within each controller.

https://app.pluralsight.com/library/courses/angular-nodejs-restful-services/table-of-contents

Loads all the controllers and routes when the server starts up
Generic way to load the controllers and the routes
A single controller encapsulating all the routes and methods - can call into the services and repository layers and implement interfaces as in any OO architecture

His project can be found here for people interested to research it more:

https://github.com/DanWahlin/AngularCLI-NodeJS-MongoDB-CustomersService

This is it, folks! Enjoy the tip and be happy!


要查看或添加评论,请登录

Eugen Frunza的更多文章

社区洞察

其他会员也浏览了