A secure MERN Stack boilerplate ready for Production that uses Docker & Nginx.

Overview

A production ready & secure boilerplate for the MERN Stack that uses Docker & Nginx.

  • Focus on the product and not the setup. You can directly start working on your idea and not worry about the nitty gritty setup.
  • Most useful libraries already installed so you don't have to run those same npm commands and configure the same thing again and again.
  • Optimized for Production out of the box with security kept in mind.
  • Ready for local development! You just need to install two requirements!

Requirements

  1. Docker - Install
  2. NodeJS - Install

Technologies Used

  • React (v18)
  • Node (v16)
  • Express (v4)
  • MongoDB (latest)
  • Nginx (v1.23.0)
  • Docker (v20.10.7)

Folder Structure

  ├── backend/
  │   ├── docker-setup/
  │   └── ...
  └── frontend/     
  │   ├── docker-setup/     
  │   └── ...
  ├── docker-compose.yml
  ├── docker-compose.production.yml

Features

Security

  1. Bcrypt is used for storing hashed passwords.
  2. Passport-JWT is used for session management.
  3. The Helmet library is used for adding the security headers to every request.
  4. Winston is used for logging the incoming request information and errors inside request handlers. The log files are compressed and are rotated every 14 days.
  5. Express-Rate-Limiter is used for limitimg the number of requests in a particular timeframe to avoid any DOS based attacks.
  6. The Joi library is used for checking and validating the params for any given Express request.
  7. Has auditjs installed as a dev dependency. Run the npm run scan command to check for any vulnerabilities in the packages installed in the Backend.
  8. Only the Backend (NodeJS) container has access to the Database (MongoDB) container.
  9. The production Dockerfiles have a non-root user created with specific permissions assigned to it.

Architecture

  1. Mounted volumes for both Frontend and the Backend for ease of development.
  2. Seperate & Optimized Docker files for Development and Production.

Backend

  1. Environment files have been setup separately for development and production using Dotenv.
  2. Mongoose is used as an object modelling framework for MongoDB.
  3. Nodemon is used to serve the Node application on the local environment for automatic reloading.
  4. Docker setup folder structure:
docker-setup/
├── mongo/
│   ├── mongo-volume
│   └── db-init.js
└── nodejs/
    ├── development/
    │   └── Dockerfile
    └── production/
        └── Dockerfile

Frontend

  1. Bootstrap used as the CSS library.
  2. SCSS compatible.
  3. React-Router enabled.
  4. Font-Awesome used as the Icon library.
  5. Axios enabled and configured as an custom interceptor that can send requests with a JWT token.
  6. React-Tostify used for showing success / error messages.
  7. Docker setup folder structure:
docker-setup/
├── nginx/
│   ├── .conf
└── react/
    ├── development/
    │   └── Dockerfile
    └── production/
        └── Dockerfile

Local Development

  • Every container has a external port that can be used for communicating with them externally.
  • Any changes made to the codebase will automatically be reflected since the volumes are mounted.
  1. Run the following command in both frontend & backend directories:
npm install
  1. Run the docker compose command:
docker compose up -d
  1. Check whether the 3 containers are running:
docker container ls
  1. The Backend APIs can be triggered by hitting the following URL:
http://localhost:5000
  1. The Frontend will be served on:
http://localhost:3000
  1. To connect any database UI software with the MongoDB container, use the following details:
Host: localhost
Port: 27018
Database Name: mern_docker_starter
Database User: local_user
Database Password: Password123

Production Setup

  • All the containers only have a internal port except the Frontend container which has ports 80 and 443 exposed.
  • The Frontend container is a multi-stage container that builds the production react build files first and then serves them using Nginx.
  • Nginx is responsible for proxying the requests based on the URL to either to the Frontend or the Backend containers.

On your production setup, follow the steps given below to run the docker containers.

  1. Change the environment variables in the .env.production file and accordingly change the database variables in the docker-compose.production.yml file.

  2. Change the localhost mentioned as server in the frontend/docker-setup/nginx/mern-template.conf file to the domain you want. Example:

server_name example.com www.example.com;
  1. Run the docker compose command with the production compose file:
docker compose -f ./docker-compose.production.yml up -d

The frontend container will be exposed on ports 80 and 443 for HTTPs.

It also has Certbot installed on it, so you can create your free SSL certificate by following the next steps:

  1. Access the frontend container's CLI
docker exec -it <frontend-container-name> bash
  1. Generate the SSL certificate using Certbot
certbot --nginx -d www.example.com -d example.com

You are all set! You should be able to access your site through your domain.

You might also like...

A production-ready ECPay AIO SDK for Node.js

node-ecpay-aio A production-ready 綠界全方位金流(ECPay All-In-One, AIO) SDK for Node.js with TypeScript Support Documentation 本模組詳細使用說明請見 User Guide Overview

Nov 1, 2022

MUI Core is a collection of React UI libraries for shipping new features faster. Start with Material UI, our fully-loaded component library, or bring your own design system to our production-ready components.

MUI Core is a collection of React UI libraries for shipping new features faster. Start with Material UI, our fully-loaded component library, or bring your own design system to our production-ready components.

MUI Core MUI Core contains foundational React UI component libraries for shipping new features faster. Material UI is a comprehensive library of compo

Dec 30, 2022

A boilerplate for ExpressJs projects configured with ESLint, Prettier & Airbnb Setup. The boilerplate utilises RESTful architecture and uses Mongodb.

A boilerplate for ExpressJs projects configured with ESLint, Prettier & Airbnb Setup. The boilerplate utilises RESTful architecture and uses Mongodb.

ExpressJs-Boilerplate An ExpressJs boilerplate configured with ESLint, Prettier & Airbnb Setup. The boilerplate utilises RESTful architecture and uses

Mar 8, 2023

⛵Generate a ready-to-use local docker environment for your Adonis application

⛵Generate a ready-to-use local docker environment for your Adonis application

Adonis Sail ⛵ Generate a ready-to-use local docker environment for your Adonis application Installation npm i --save-dev adonis-sail node ace configur

Nov 27, 2022

My-portfolio - Built with Namecheap, Digital Ocean, Nginx, PM2, SSL, NextJs, Tailwind 3, Graphql, NexusJS, Prisma, Postgres, Passion and Love

Current Implementation technologies Nextjs with Typescript. Static pages/ Server side rendering. Easy peasy state management (Might not need it with i

Jan 10, 2022

Automated packaging of Debian-flavored NGINX with PageSpeed modules. Written in Bash and GitHub Workers. APT Repository hosted on Dokku.

NGINX + Google PageSpeed Configuring NGINX to build correctly is a pain. Not because of anything wrong with it, but rather because of how slim the sta

Oct 14, 2022

API of my readings, developed in Nest.js, MongoDB, Nginx and Dockerized

API of my readings, developed in Nest.js, MongoDB, Nginx and Dockerized

My Readings step by step for configuration with docker OBS: required docker and docker-compose cp -r .env.sample .env docker-compose up example what t

Jul 1, 2022

A Turborepo with Strapi v4 (w/ postgres database) + Next.js powered by docker and docker-compose

A Turborepo with Strapi v4 (w/ postgres database) + Next.js powered by docker and docker-compose

Turborepo + Strapi + Next.js + Docker A Turborepo with Strapi v4 (w/ postgres database) + Next.js powered by docker and docker-compose. ⚠️ Prerequisit

Dec 29, 2022

Docker Extension to backup and restore docker volumes.

Docker Extension to backup and restore docker volumes.

vackup-docker-extension This repository contains a Docker Extension that backups Docker volumes. This extension is purely a demonstration of how it'd

Dec 28, 2022
Owner
Karan Jagtiani
SDE I at HackerRank, @interviewstreet | Full Stack | DevOps
Karan Jagtiani
This is a full-stack exercise tracker web application built using the MERN (MongoDB, ExpressJS, ReactJS, NodeJS) stack. You can easily track your exercises with this Full-Stack Web Application.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

WMouton 2 Dec 25, 2021
A Secure Web Proxy. Which is fast, secure, and easy to use.

Socratex A Secure Web Proxy. Which is fast, secure, and easy to use. This project is under active development. Everything may change soon. Socratex ex

Leask Wong 220 Dec 15, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Online Inventory Control System for an apparel manufacturing company "CASANOVA" (Pvt) Ltd. Technology stack: Node.js, Express.js, MongoDB Atlas, React.js (MERN Stack).

Project Name - Online Inventory Control System for an apparel manufacturing company "CASANOVA". The project was given a "A" grade. Group Leader - IT20

Pasindu Rukshan 1 Dec 26, 2021
It is a solo Project and In this repo I try to build a E-Commerce full-stack website with MERN stack technologies. For Practice purpose.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Alok Kumar 5 Aug 3, 2022
The high efficent browser driver on top of puppeteer, ready for production scenarios.

browserless is an efficient driver for controlling headless browsers built on top of puppeteer developed for scenarios where performance matters. High

microlink.io 1.2k Jan 6, 2023
Prototype of real-time comments and a proposal of how to make it "production-ready".

Real-time comments prototype Simple demonstration of real-time commenting. Installation After forking it, run npm install, then you need two environme

Tiger Abrodi 3 Jan 16, 2022
A highly opinionated and complete starter for Next.js projects ready to production

The aim for this starter is to give you a starting point with everything ready to work and launch to production. Web Vitals with 100% by default. Folder structure ready. Tooling ready. SEO ready. SSR ready.

Fukuro Studio 28 Nov 27, 2022