The structure of nodejs project is influenced by the private preferences, project’s architecture and module injection strategy getting used .Also on event based arc’ which uses dynamic module instantiation mechanism.

To have a MVC structure it’s imperative to filter the server side and client side ASCII text file because the client side code will probably be minimized and sent to browser and is public in its basic nature.

And the server side or backend will provide API to perform CRUD operations.

A simple nodejs application with MVC and API

The first major distinction is between the dynamically generated directories which will be used for hosting and source directories.

The source directories will have a config file or folder depending on the amount of configuration you may have.This includes the environment configuration and business logic configuration which you’ll choose to put inside config directory.

 |-- Config  

       |-- config.json 
        |-- appConfig 

                  |-- pets.config 
                  |-- payment.config 

Now the foremost vital directories where we distinguish between the server side/backend and therefore the frontend modules .

The 2 directories server and webapp represent the backend and frontend respectively which we can prefer to put inside a source directory viz. src.Inside the server directory you’ll have the controller ,the App.js/index.js which can be you main nodejs file and start point .

The server dir. can also have the dto dir which holds all the info transfer objects which can be usd by API controllers.

 |-- server

       |-- dto 

            |-- pet.js 
            |-- payment.js 

       |-- controller  

            |-- PetsController.js 
            |-- PaymentController.js

       |-- App.js 

The webapp directory are often divided into two major parts public and mvc , this is often again influenced by what build strategy you want to use. We are using browserfiy the build the MVC a part of webapp and minimize the contents from mvc directory simply put.

 |-- webapp |-- public |-- mvc 

Now the public directory can contain all the static resources,images,css(you can have saas files as well) and most importantly the HTML files .

 -- public

     |-- build // will contianed minified scripts(mvc) 
     |-- images  

           |-- mouse.jpg  
           |-- cat.jpg  
 |-- styles  
       |-- style.css 
 |-- views 
       |-- petStore.html
       |-- paymentGateway.html
       |-- header.html 
       |-- footer.html 
 |-- index.html 

The mvc directory will contain the front-end logic including the models,the view controllers and any other utils modules you may need as part of UI. Also the index.js or shell.js whichever may suite you is part of this directory as well .

 |-- mvc  
       |-- controllers 
       |-- Dashborad.js 
       |-- Help.js 
       |-- Login.js  
  |-- utils 
  |-- index.js 

So in conclusion the entire project structure will look like below.And a simple build task like gulp browserify will minify the mvc scripts and publish in public directory.

We can then provide this public directory as static resource via express.use(satic(‘public’ )) api.

 |-- node_modules  
 |-- src 
       |-- server  
              |-- controller 
              |-- App.js // node app
       |-- webapp  
            |-- public 
                 |-- styles  
                 |-- images  
                 |-- index.html 
        |-- mvc 
              |-- controller 
              |-- shell.js // mvc shell  
   |-- config  
   |-- Readme.md 
   |-- .gitignore 
   |-- package.json 

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Project Structure in Node.js

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password