As I look at other people's nodeJS repository, I see neatly organized code and folder structure by people who seem like they really know what they're doing. Meanwhile I look at mine, it's a mess. As convenient as it might be to not having keeping everything in a single file, it's not the best practice. So I set on mission to perfectly organize my folder structure.
To do that, I went through a bunch of other people's repositories, read a bunch of articles and basically what I learned was there is no one-size-fits-all approach. However in my case, I did end up with a folder structure that I think will make my code somewhat maintainable.
In case of my project, I'm doing a full stack javascript application with vuejs at the front-end and nodejs at the backend.
App folder:
->client (consists of the client side VueJS)
->server
--->controllers
--->routes
--->models
---services
--->helpers
--->server.js (app entry point)
->.env / . git (and other stuff)
Here I'm only focusing on the server side nodejs. The main components are :
- services: consists of methods that does the CRUD(Create Read Update Delete) dirty work
- controllers: takes user request (HTTP requests in our case) and tells the services what to do
- models: represents the database schema
- routes : were done using express to connect urls (user's get and post requests) to controllers
- helpers: Although there is no clear cut definition in terms of javascript, according to people programming in Java helpers are basically a bunch of functions that help in the internal working of the app, but do not directly serve the user's requests. So it would be safe to assume that it would do the same in Javascript.
So this is how you can organize you Nodejs folder structure for your project.
Thanks for reading and would would love to hear any insights into this matter.
Follow me on Twitter: @Nipeshkc*
Visit my blog: blog.arpankc.com
Top comments (2)
We call it utilities. Suppose there is one function to catch the unique key constraint of sql server. This function is not dependent specifically to what project you are working per se. And can be copied and used in any projects that use sql servers.
Also,suppose you have to use some js library many times to perform some logic. For example use ramda intersection method to find the common items in two list. You can abstract this logic with your own "helper" function so that you dont import ramda in every file it requires.
Get the gist? Helpers function are something that's independent to your current project and something you create to make your code reusable and your life lot easier. Common helper function could be like formatting the given date, checking if the given date is this week etc.
Hope this helps you understand what helper functions are. Cheers!
First of all, sorry for the late response. And second this really clears a lot of stuff for me and will definitely be making some changes to my current project structure. Thanks !!!