Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Arpan Kc
Arpan Kc

Posted on • Edited on

     

Organizing my NodeJS code and folder structure

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)
Enter fullscreen modeExit fullscreen mode

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: 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
eddie023 profile image
Manish Chaulagain
  • Joined

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!

CollapseExpand
 
nipeshkc7 profile image
Arpan Kc
Full-Stack Software Engineer. Blogs at https://blog.arpankc.com
  • Email
  • Location
    Australia
  • Education
    University of Wollongong
  • Joined

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full-Stack Software Engineer. Blogs at https://blog.arpankc.com
  • Location
    Australia
  • Education
    University of Wollongong
  • Joined

More fromArpan Kc

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp