Node Hero - Node.js Project Structure Tutorial - RisingStack Engineering (2024)

This is the 7th part of the tutorial series called Node Hero – in these chapters, you can learn how to get started with Node.jsNode.js is an asynchronous event-driven JavaScript runtime and is the most effective when building scalable network applications. Node.js is free of locks, so there's no chance to dead-lock any process. and deliver software products using it.

Most Node.js frameworks don’t come with a fixed directory structure and it might be challenging to get it right from the beginning. In this tutorial, you will learn how to properly structure a Node.js project to avoid confusion when your applications start to grow.

See all chapters of the Node Hero tutorial series:

  1. Getting started with Node.js
  2. Using NPM
  3. Understanding async programming
  4. Your first Node.js HTTP server
  5. Node.js database tutorial
  6. Node.js request module tutorial
  7. Node.js project structure tutorial[ this article ]
  8. Node.js authentication using Passport.js
  9. Node.js unit testing tutorial
  10. Debugging Node.js applications
  11. Node.js Security Tutorial
  12. How to Deploy Node.js Applications
  13. Monitoring Node.js Applications

UPDATE: We wrote another article aboutNode.js project structuring, which discusses advanced techniques as well.

The 5 fundamental rules of a Node.js Project Structure

There are a lot of possible ways to organize a Node.js project – and each of the known methods has their ups and downs. However, according to our experience, developers always want to achieve the same things: clean code and the possibility of adding new features with ease.

In the past years at RisingStack, we had a chance to build efficient Node applications in many sizes, and we gained numerous insights regarding the dos and donts of project structuring.

We have outlined five simple guiding rules which we enforce during Node.js development. If you manage to follow them, your projects will be fine:

Rule 1 – Organize your Files Around Features, Not Roles

Imagine, that you have the following directory structure:

// DON'T.├── controllers| ├── product.js| └── user.js├── models| ├── product.js| └── user.js├── views| ├── product.hbs| └── user.hbs

The problems with this approach are:

  • to understand how the product pages work, you have to open up three different directories, with lots of context switching,
  • you end up writing long paths when requiring modules:require('../../controllers/user.js')

Instead of this, you can structure your Node.js applications around product features / pages / components. It makes understanding a lot easier:

// DO.├── product| ├── index.js| ├── product.js| └── product.hbs├── user| ├── index.js| ├── user.js| └── user.hbs

Rule 2 – Don’t Put Logic inindex.jsFiles

Use these files only to export functionality, like:

// product/index.jsvar product = require('./product')module.exports = { create: product.create}

Rule 3 – Place Your Test Files Next to The Implementation

Tests are not just for checking whether a module produces the expected output, they also document your modules(you will learn more on testing in the upcoming chapters). Because of this, it is easier to understand if test files are placed next to the implementation.

Put your additional test files to a separatetestfolder to avoid confusion.

.├── test| └── setup.spec.js├── product| ├── index.js| ├── product.js| ├── product.spec.js| └── product.hbs├── user| ├── index.js| ├── user.js| ├── user.spec.js| └── user.hbs

Rule 4 – Use aconfigDirectory

To place your configuration files, use aconfigdirectory.

.├── config| ├── index.js| └── server.js├── product| ├── index.js| ├── product.js| ├── product.spec.js| └── product.hbs

Rule 5 – Put Your Long npmnpm is a software registry that serves over 1.3 million packages. npm is used by open source developers from all around the world to share and borrow code, as well as many businesses. There are three components to npm: the website the Command Line Interface (CLI) the registry Use the website to discover and download packages, create user profiles, and... Scripts in ascriptsDirectory

Create a separate directory for your additional long scripts in package.json

.├── scripts| ├── syncDb.sh| └── provision.sh├── product| ├── index.js| ├── product.js| ├── product.spec.js| └── product.hbs

Next up

In the next chapter of Node Hero, you are going to learn how toauthenticate users using Passport.js. Until the next chapter comes out, feel free to ask any questions you ran into!

UPDATE: We wrote another article aboutNode.js project structuring, which discusses advanced techniques as well.

Node Hero - Node.js Project Structure Tutorial - RisingStack Engineering (2024)
Top Articles
Rules For Helping Your College Student Adjust
The log management process | Best practices revealed | Sumo Logic | Sumo Logic
Sdn Md 2023-2024
Valley Fair Tickets Costco
Eric Rohan Justin Obituary
CKS is only available in the UK | NICE
Routing Number 041203824
Kent And Pelczar Obituaries
Roblox Character Added
Www.paystubportal.com/7-11 Login
Jet Ski Rental Conneaut Lake Pa
Miami Valley Hospital Central Scheduling
iOS 18 Hadir, Tapi Mana Fitur AI Apple?
VMware’s Partner Connect Program: an evolution of opportunities
Dr Manish Patel Mooresville Nc
Grab this ice cream maker while it's discounted in Walmart's sale | Digital Trends
Troy Bilt Mower Carburetor Diagram
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Noaa Ilx
Self-Service ATMs: Accessibility, Limits, & Features
Village
Hdmovie2 Sbs
Speedstepper
Dexter Gomovies
Craigslist Brandon Vt
Ts Modesto
Greater Orangeburg
Current Time In Maryland
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
Cars And Trucks Facebook
Moxfield Deck Builder
Today's Final Jeopardy Clue
Back to the Future Part III | Rotten Tomatoes
Omnistorm Necro Diablo 4
Bimar Produkte Test & Vergleich 09/2024 » GUT bis SEHR GUT
Kelly Ripa Necklace 2022
Body Surface Area (BSA) Calculator
Pp503063
R/Moissanite
Jack In The Box Menu 2022
Anhedönia Last Name Origin
More News, Rumors and Opinions Tuesday PM 7-9-2024 — Dinar Recaps
Samantha Lyne Wikipedia
Gravel Racing
Unitedhealthcare Community Plan Eye Doctors
Yakini Q Sj Photos
Doe mee met ons loyaliteitsprogramma | Victoria Club
Sherwin Source Intranet
Rovert Wrestling
Houston Primary Care Byron Ga
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 5542

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.