lundi 27 juin 2022

Service and Controller Layers, Business Logic and Project Structure with Node.js

I come from a decade of experience with Java EE, SE and Java with Spring. If there's something that was drilled in me by myself and other fellow developers, was how to make use of design patterns, separation of responsibility, separating definition from implementation, interface segregation, etc. Also, we were always worried about testable code (unit testing and integration tests).

When I was learning Java, there wasn't a single book, magazine or website that didn't implemented things with those rules in mind, to the point of boredom. So we always new how things should be done from an architectural point of view when starting new stuff or maintaining code. With time, projects like Spring Boot and JBoss Seams started to give you out of the box a basic project layout witch you simply followed to success(or at least you should...).

Now that I delve deep in Node.js, I miss that so much. There seems to be only a handful (and I mean it! google it and you'll see...) of people on the web worried about it and trying to teach newcomers how to write good backend code with Node.js.

The smashing majority of people teach you how to boot express and put all of your code (database, logging, mailing, error handling and so on) inside an express route and they walk out of it fine, as if they have done a great service to mankind, and you supposedly are now already able to go out and find a good paying job for yourself.

Of course, when I started learning Java, there was nothing preventing you from using Servlets directly with some JDBC code thrown inside, or even worse, Scriptlet inside JSPs... But if you did that, that meant you were lame, slow-witted, unfit for serious duty, or suffered from some kind of brain damage. The cool guys wouldn't want to be seen around in your company, and you would have lunch alone in the cantina every day, on a table reserved only for yourself.

So here I am, asking please, can you guys point me into the right direction? Or at least, away from the wrong ones. I would appreciate so much to know how you guys for instance implement a service layer, where do you put your business logic? that sort of thing... Do you put it in classes or functions that returns other functions? How to write easily testable code by Mocha for instance... Do I really need a controller layer, or can I trust my routes to cover that responsibility? I'm totally lost on that. Of course, I've come around with some solutions of my own (some even silly...), but I want to hear from the pro, from the battled people, from the veterans.

I'm interested in JavaScript only on the Backend, using Express.js, Socket.io, RabbitMQ, Passport.js, and GraphQL(in the future). Not interested in any rendering engine or template engines, much less JavaScript on the Browser. I'm already very proficient with Sequelizer and Mongoose.

Thank you guys and girls so much and sorry for my frustration, I hope you guys understand, and could perhaps even relate.

Aucun commentaire:

Enregistrer un commentaire