Skip to content

Rohail1/nodejs-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js BoilerPlate

This project is basic boilerplate for a complete professional structure of a nodejs project.

How To Use

Libs

Libs folder contains some external libraries and some internal helper files. Currently there are two files in libs folders. Helper.js contains helper methods used throughout the app. The other file is dependencies.js this file contains all the dependencies and external modules all required in one single file so it can be passed to API files and used via destructuring to get required modules.

Configs

Configs folder contains all the configuration for the project.

Middlewares

Add your Route-level or App-level middlewares in the Middlewares directory in their respective files.

/Middlewares
  ->index.js
  ->appLevel.js
  ->routeLevel

Make sure the order of your APP level middleware is correct as it follows First-In-First-Out Orders so the middleware placed first in the array will be used first in the app.
As for Route level middlewares you can register them when you register the route.

APIs

For writing APIs create a file in the APIs folder By following the structure of the TestAPI.js File And the routes and their middleware will automatically be registered to the Application. I have passed all the dependencies via object to the APIs each of those can be accessible via destructuring the paramenters of the Setup function. Look into TestAPI.js file for furthor details

  module.exports.APIs = {

    getUsers : {
      route : '/users',
      method : 'GET',
      prefix : config.API_PREFIX.API,
      middlewares : [middlewares.dummyRouteLevelMiddleware2],
      handler : getUsers
    },
    deleteUser : {
      route : '/users/:userId',
      method : 'DELETE',
      prefix : config.API_PREFIX.API,
      middlewares : [middlewares.getParams], //FIFO order of middleware
      handler : deleteUser
    }

  };

Validators

Name the validator file should be same as the API file for which the validators are being created and they will automatically be imported in the API file through parameters. I have used Joi for validation.Its great validation library.

Models

Models folder contains mongoose models for your schemas.Add new model schemas and register/require them in Models/index.js so they can be loaded and exposed to entire app.

Envs

This folder contain env files for each environment i.e development, production and staging. Make sure to add this folder in .gitignore ! In these files you can specify your db connection strings, port and API credentials or keys. Once decleared a variable in env file it will be accessible throught the app via process.env varaible.