Skip to content

Application folder structure design to quickly implement Rest APIs using Node.js, Express.js and MongoDB

License

Notifications You must be signed in to change notification settings

jaimin1618/nodejs-backend

Repository files navigation

Node.js backend application - project structure setup

My preferred Node.js backend project setup. Codebase is organized keeping in mind both development and production environments.

Table Of Contents

Getting started

Prerequisites

Basic familarity with Git, Node.js and npm.
Basic familarity with server-side application development using Node.js and Express.js.
Basic familarity with development and production environments for Node.js backend applications.

Tech Stack

Server: Node.js & xpress.js
Database: MongoDB

Installation

Clone the project

git clone https://github.com/jaimin1618/nodejs-backend.git

Go to the project directory

cd nodejs-backend

Install dependencies

npm install

Setup environment variables & fill environment variables in .env file.

cp .env.example .env

Start the server

npm run server

Usage and examples

development phase practices.

  • Keep the server.js file as it is during development stage.

  • Configure environment variables .env and constants file config/constants.js

  • Organize endpoints/routes in multiple files & wrap the controller with AsyncHandler, it will deal with try {} catch(e) {} wrapping of async program errors.

    router.use(
      "/",
      AsyncHandler(async (req, res) => {
        res.json(
          ApiResponse("Api Running Successfully.", null, StatusCodes.CREATED)
        );
      })
    );
    
  • Use common api response structure for all endpoints - as shown in below example.

    res.json(
      ApiResponse("message", data, StatusCode)
    );
    
  • Use common api error response structure to throw errors - as shown in below example.

    if (!authenticate) {
      throw new ApiError("Invalid credentials!", StatusCodes.UNAUTHORIZED, {
      credentials: { email, password },
      });
    }
    
  • Let the middleware handle BAD_REQUEST errors.

    router.post(
      "/login",
      AuthRoutesValidations.loginValidation, // request validators
      HandleBadRequest, // BAD REQUEST - 400 middleware handler
      AuthController.login
    );
    

Moving to production stage.

  • Get the SSL certificate files chain.pem, privkey.pem and cert.pem and put them inside config/ folder. You can use Certbot CLI for getting SSL certificate from letsencrypt.
  • Uncomment the lines with comment "for production use" inside server.js file.
  • Configure environment variables inside .env file.
  • Start the server with following commands.
npm run server

or using PM2 - Node.js process manager

sudo pm2 start server.js --name="server"

Contributing

Contributions are always welcome! If you'd like to contribute to this project, please follow these guidelines to ensure a smooth and collaborative process.

Setup project locally

  1. Fork the repository to your GitHub account.

  2. Clone the forked repository to your local machine:

git clone https://github.com/jaimin1618/nodejs-backend.git
  1. Clone the forked repository to your local machine:
git checkout -b feature/your-feature
  1. Make your changes, additions, or bug fixes.

Submitting a Pull Request

  1. Commit your changes with a descriptive commit message:
git commit -m "Add feature: your-feature"
  1. Push your changes to your forked repository:
git push origin feature/your-feature
  1. Create a pull request (PR) from your branch to the main repository. Be sure to provide a clear and detailed description of your changes in the PR. Participate in the discussion and address any feedback or comments received on your PR.

Feedback

If you have any feedback, please reach out to me at [email protected]

License

MIT

Authors

Releases

No releases published

Packages

No packages published