Why Laravel is better then NodeJs/Express?
Published: 2024-07-07 06:33:00

Hello WebDevs,
First thing first, JS devs have more salary then PHP devs on average.
I've come from Laravel background and recently due to less salary in php environment I switched to JS environment NodeJs/Express. You can get more salary in php but that require other knowledge like DSA, SQL or Project Management. 

If you are a Startup which has less time to go to market, want's less overhead tension and want cheaper employees then always prefer Laravel.

The major drawback of using JS is there's no batteries included framework in JS, you have to search a good package with good documentation and actively maintain. Then learn that library. Then if in future it's not maintained or any security issue came then repeat the process again.
While in batteries included framework majority of features will be maintained till lifetime of framework so we don't have to worry a bit.
 
Woof that was lot from my heart and not to be made public but will help you guys make the right decision.

I will share my real world experience of onboarding to Express project. 
Let's compare implementation difference both the frameworks.

**1. Language**
- Laravel is created from PHP one of the most used programming language in backend.
- Express is created in JS, a functional programming language made for browsers running frontend.
- Although you can do functional programming in PHP and OOP in JS, it's not that much supportive and easy.
- The only major benefit of JS is speed due to it's asynchronous nature.
- You can return the response while not waiting for other functions like logging or saving record/file
- While most of the time you have to take care of async-await as well, for the same reason.
- Php will just perform task synchronously, There's no such concept of promises or async/await here. Also due to same reason PHP is more easy to grab then JS.
- For performing background task in Laravel, We have Jobs and Queues.
- Another major difference between both languages is PHP provides optional type checking, while JS doesn't provide any type checking or else you have to use TypeScript for strict type checking.

**2. Routes**
- Express provides mostly routing features.
- Both frameworks provide use of separate files with name, middleware and prefix.  
- Laravel provide Route grouping and in node you have to use another package and learn it.
- Laravel provide RouteBinding in controller parameters, It can even throw 404 if data not found in database. In Express you have to do it manually and add that data to request body, there's no concept of controller parameter binding or DB checking.
- Laravel provide clear separation between web, api, console and channel route, A backend application generally needs all this. While in express you have to learn and implement all by yourself. If a beginner don't know what channel and console routes are Laravel will give opportunity to learn while express will not push him/her for learning.

**3. Middleware**
In Express every callback you provide to route is middleware, there's no clear difference between controller and middleware part.
A controller business logic is always unique for some request while middleware is peace of common to multiple request and always need to be run first before or after controller. If you give express to beginner they may put middleware after request validation or controller in Express but in Laravel it's not possible.

**4. Request validation**
Laravel provides simple yet powerful solution for request validation like FormRequest which provides feature like authorization, validation messages, pre-validation request update and post-validation, 
While in Express you have to choose, learn and implement a package.
then also you will not find features like FormRequest in same package.

**5. ORM**
Express provide no ORM. When I started express I searched for good query builder and ended up with Sequalize, then learned it via documentation, then struggled a lot to implement it. then my friend told me it's not actively maintained. And then I find out recent commits in github were done by bots. Then again I found another query builder like knex, then learnt it, then also it was not as good as Eloquent ORM and Laravel DB query builder provided by default.

**6. Logging**
Last but not least logging, Laravel provide default and simple logging while I have to struggle a lot to implement logging via another third party package called winston. then for keeping separate files for separate dates I have to again install another package called daily-rotate. In both framework you can install extra package to log into other channels like email or slack.

In the end both are legendary frameworks, there's no hate for any framework, I only shared my experience, your experience and use case might be different. Please let me know If I'm wrong anywhere.

I hope you enjoyed reading this.
Thanks and regards,
Yogesh Galav