Go to Home

Laravel like Controller in Express.js

After working in laravel for almost 5 years now, I had recently been experimenting my hands with Node.js. So, I start with super great Node.js course by Mosh and found Node.js to be fairly simple one considering my experience with js. While developing a basic crud application using Express.js and MongoDB I kinda felt the routes file becoming big since I was encapsulating all the controller and model logic inside the same route file. So, I decided to extract the controller and model logic outside the routes file like how we would do with laravel

And following is what I did to refactor the code

By implementing the above mentioned flow, I can now segregate all the user related routes under the UserController file and it makes my route file pretty clean and neat. Plus, I can also implement basic resourceful controller flow like index, create, store, show, edit, update and delete under the same controller. Hope you find this useful.

See you soon in another tutorial. Happy Coding!