loading...

Organizing my NodeJS code and folder structure

nipeshkc7 profile image Arpan Kc Updated on ・2 min read

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. How messy you ask? the routes/controllers/services/all in one single index.js kind of mess. As convenient as it might be to not having bunch of tabs open, 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 :

  1. services: consists of methods that does the CRUD(Create Read Update Delete) dirty work
  2. controllers: takes user request (HTTP requests in our case) and tells the services what to do
  3. models: represents the database schema
  4. routes : were done using express to connect urls (user's get and post requests) to controllers
  5. helpers: ???

I had no clue what helpers were. Did some research could not find a clear cut definition in terms of Javascript. However people programming in Java say they are basically a bunch of functions that help in the internal working of the app, however do not directly serve the user's requests. So I take it that it would do the same in Javascript.

So this is how I'm going to organize my Nodejs folder structure for my project.

Thanks for reading and would appreciate any insights into this matter.

P.S. Please follow me on twitter, I'd really appreciate it. @Nipeshkc

Posted on Feb 2 by:

Discussion

markdown guide
 

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 !!!