Months ago I began a new web project which, in the beggining, seemed like a small application with virtually few users. I began the project by using the awesome Hackathon Starter WebApp Boilerplate by Sahat because the initial requirements seemed to fit into that boilerplate. As you may expect, the project requirements have evolved since then and right now I have a large-scale SaaS application. The front end code was completely decoupled from the initial project in order to use React, and I striped out everything from the original project to leave it only as a back end REST API service. Right now, I need to implement a bunch of features and the project's codebase seems to have become a full Bolognese Spaghetti code which I need to refactor ASAP.

For the past 2 months I've been diving into architecture design books, posts and videos, but my app keeps growing without me being able to make a decision and refactor the back end code. Right now, it is just plain MVC with mongoose on the Models, express on the server and everything from business logic, to authentication, validation and data presentation inside the Controllers.

I've tried (literally) thousands, of different back end boilerplates in multiple languges and specs, but I would like to keep coding in NodeJS (TypeScript welcome). My question is, for a newbie with no experience on large-scale apps, how to refactor/rewrite my code, into a more robust architecture using NodeJS. Should I use ORMs? Should I use DDD, Service/Repository? Authentication with JWT should be a middleware? Using RBAC is acceptable? Also, the database design on MongoDB became so difficult I (literally yesterday) switched to PostgreSQL as my data began to become more relational than I anticipated. I didn't expect this project to grow this big and I am panicking as I can't afford rewriting/refactoring code more than once at least for the next 8 months as I will be the only developer involved in the back end side.

Thanks in advance and sorry for my awful english.

  • Good start is to move your controller logic into injectable Service/Repository structure. Then later, you can start adding some DDD to it by enriching your domain models. As always, start small, write some unit tests, and keep refactoring all the time. – bobek Jan 24 '19 at 17:11
  • Thanks @HubertJarema! Should I go for entire refactor to add Service/Repository or should it be a gradual change? – HumbertoWoody Jan 24 '19 at 17:32
  • @HumbertoWoody gradual. There's a risk of introducing bugs as you change the code. It's safer to modify it as you have new features you need to implement. As you learn more about the domain and future of the software, you'll have less coupled to what is being refactored and can more easily shape it to whatever is needed. – bitsoflogic Jan 24 '19 at 17:44
  • Thanks @bitsoflogic! I am aware of some awful bugs that actually exist on the project, is it better to focus on fixing them with the previous approach? Or fix them with new architecture? :) – HumbertoWoody Jan 24 '19 at 18:08
  • Definitely, gradual. And start adding unit tests. If your code is covered with tests you can refactor it any time, and be fairly certain you didn't break anything. – bobek Jan 24 '19 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.