/Express-Starter

:truck: A boilerplate for Node.js, Express, Mongoose, Heroku, Atlas, Nodemon, PM2, and Babel. REST / GraphQL API Server | PaaS | SaaS | CI/CD | Jest | Supertest | Docker | MongoDB | PostgreSQL | Sequelize | Lodash | RxJS | JWT | Passport | WebSocket | Redis | CircleCI | Apollo | DevSecOps | Microservices | Backend Starter Kit | ES6

Primary LanguageJavaScriptMIT LicenseMIT

Express Starter

🚚 A boilerplate for Node.js, Express, Mongoose, Heroku, Atlas, Nodemon, PM2, and Babel.

Build Status Coverage Status // Dependency Status devDependency Status

🌈 Live Demo

This seed repository provides the following features:

  • ---------- Essentials ----------
  • Web application framework with Express.
  • Object-document mapping with Mongoose.
  • Make authenticated requests with Passport.
  • File upload with Multer.
  • Real-time communication with WS.
  • ---------- Tools ----------
  • Next generation JavaScript with Babel.
  • JavaScript static code analyzer with ESLint.
  • Code formatter with Prettier.
  • Unit testing with Jest.
  • End-to-End testing with Supertest.
  • Mocking external requests with Nock.
  • Automatically restart application with Nodemon.
  • Keeping application alive with PM2.
  • Reverse proxy with Caddy.
  • ---------- Environments ----------
  • Cloud application hosting with Heroku.
  • Cloud NoSQL database hosting with Atlas.
  • Cloud storageβ€Ž hosting with Cloudinary.
  • Error tracking service with Sentry.
  • Software container with Docker.
  • Continuous integration with CircleCI.
  • Fix and prevent known vulnerabilities with Snyk.
  • Test coverage integration with Codecov.

Table of Contents

Getting Started

Follow steps to execute this boilerplate.

  1. Clone this boilerplate
$ git clone --depth 1 https://github.com/Shyam-Chen/Express-Starter.git <PROJECT_NAME>
$ cd <PROJECT_NAME>
  1. Install dependencies
$ npm install
  1. Start a development server
$ yarn serve
  1. Produce a production-ready bundle
$ yarn build
  1. Lint and fix files
$ yarn lint
  1. Run unit tests
$ yarn unit
  1. Run end-to-end tests
$ yarn e2e
  • MongoDB
$ brew tap mongodb/brew
$ brew install mongodb-community
$ mongo --version
# MongoDB shell version v4.4.1
# Build Info: {
#     "version": "4.4.1",
#     "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
#     "modules": [],
#     "allocator": "system",
#     "environment": {
#         "distarch": "x86_64",
#         "target_arch": "x86_64"
#     }
# }

# Starting MongoDB
$ brew services run mongodb-community
$ brew services list

# Stopping MongoDB
$ brew services stop mongodb-community

Dockerization

Dockerize an application.

  1. Build and run the container in the background
$ docker-compose up -d app
  1. Run a command in a running container
$ docker-compose exec app <COMMAND>
  1. Remove the old container before creating the new one
$ docker-compose rm -fs
  1. Restart up the container in the background
$ docker-compose up -d --build app

Local Databases

$ docker-compose up -d mongodb

Configuration

Control the environment.

Default environments

Set your local environment variables.

// src/env.js

export const NODE_ENV = process.env.NODE_ENV || 'development';
export const INDEX_NAME = process.env.INDEX_NAME || 'local';

export const HOST = process.env.HOST || '0.0.0.0';
export const PORT = process.env.PORT || 3000;

export const SECRET_KEY = process.env.SECRET_KEY || 'jbmpHPLoaV8N0nEpuLxlpT95FYakMPiu';

export const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/test';
export const REDIS_URL = process.env.REDIS_URL || 'redis://127.0.0.1:6379/4';

// ---

export const CLOUDINARY_URL = process.env.CLOUDINARY_URL || 'cloudinary://key:secret@domain_name';

export const RATE_LIMIT = process.env.RATE_LIMIT || 0;

export const SENTRY_DSN = process.env.SENTRY_DSN || null;

Continuous integration environments

Add environment variables to the CircleCI build.

# Project Settings > Environment Variables > Add Environment Variable

SECRET_KEY
MONGODB_URI
SENTRY_DSN

Examples

Directory Structure

The structure follows the LIFT Guidelines.

.
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ core
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ <FEATURE> -> feature modules
β”‚   β”‚   β”œβ”€β”€ __tests__
β”‚   β”‚   β”‚   β”œβ”€β”€ controller.spec.js
β”‚   β”‚   β”‚   β”œβ”€β”€ service.spec.js
β”‚   β”‚   β”‚   β”œβ”€β”€ model.spec.js
β”‚   β”‚   β”‚   └── rest|<FLOW>.e2e-spec.js
β”‚   β”‚   β”œβ”€β”€ controller.js
β”‚   β”‚   β”œβ”€β”€ service.js
β”‚   β”‚   β”œβ”€β”€ model.js
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ <GROUP> -> module group
β”‚   β”‚   └── <FEATURE> -> feature modules
β”‚   β”‚       β”œβ”€β”€ __tests__
β”‚   β”‚       β”‚   β”œβ”€β”€ controller.spec.js
β”‚   β”‚       β”‚   β”œβ”€β”€ service.spec.js
β”‚   β”‚       β”‚   β”œβ”€β”€ model.spec.js
β”‚   β”‚       β”‚   └── rest|<FLOW>.e2e-spec.js
β”‚   β”‚       β”œβ”€β”€ controller.js
β”‚   β”‚       β”œβ”€β”€ service.js
β”‚   β”‚       β”œβ”€β”€ model.js
β”‚   β”‚       └── index.js
β”‚   β”œβ”€β”€ app.js
β”‚   β”œβ”€β”€ env.js
β”‚   └── server.js
β”œβ”€β”€ .editorconfig
β”œβ”€β”€ .eslintrc
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .prettierrc
β”œβ”€β”€ babel.config
β”œβ”€β”€ Caddyfile
β”œβ”€β”€ circle.yml
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ jest.config.js
β”œβ”€β”€ LICENSE
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ package.json
β”œβ”€β”€ processes.js
└── README.md

Microservices

Microservice architecture – a variant of the service-oriented architecture structural style – arranges an application as a collection of loosely coupled services. In a microservices architecture, services are fine-grained and the protocols are lightweight.

See Server-side Micro-Fullstack for instructions on how to create microservices from source code.