express-mongo-typescript-boilerplate

A production-ready boilerplate based on Node.js, Express and MongoDB (using Mongoose ODM) written in TypeScript. It gets you up and running very fast to quickly build RESTful APIs.

MIT License

Stars
10

Features

Run the app

Locally

Install dependencies: npm install or yarn

Start development server using Nodemon: npm run dev

Keep in mind that Docker is not used here. Instead, the environment variable DATABASE_URI gets accessed.

Docker(-Compose)

docker-compose up

Docker-Compose automatically creates a Node.js container using the Dockerfile as well as a MongoDB container using the mongo:4.4.5-bionic image.

Start the Node.js container only docker build <project-dir> -t node-app

Environment variables

The NODE_ENV environment variable is set inside nodemon.json for development and in the Dockerfile for production. If you don't use Docker in production, you can set the NODE_ENV variable in the ecosystem.config.json file (PM2 config file) like so:

{
  "apps": [
    {
      "name": "app",
      "script": "index.js",
      "instances": 1,
      "autorestart": true,
      "time": true,
      "env_production": {
        "NODE_ENV": "production"
      }
    }
  ]
}

SERVER_PORT=3000

DATABASE_URI=mongodb://127.0.0.1:27017/express-boilerplate

JWT_SECRET=your-secret
JWT_EXPIRATION_HOURS=24
JWT_ISSUER=Express Boilerplate

SMTP_HOST=mail-server
TP_PORT=587
SMTP_USERNAME=mail-server-username
SMTP_PASSWORD=mails-server-password
EMAIL_FROM=mail-sender

Project Structure

src\
 |--config\         # Environment variables and configurations
 |--controllers\    # Route controllers
 |--middlewares\    # Middlwares
 |--models\         # Mongoose models
 |--routes\         # Routes
 |--services\       # Business logic
 |--types\          # Custom types
 |--app.ts          # Express app
 |--index.ts        # Entry point

API

Endpoints

Auth routes

POST /v1/auth/register - body: { email: string, password: string }

POST /v1/auth/login - body: { email: string, password: string }

GET /v1/auth/me

GET /v1/auth/logout

User routes

GET /v1/users

GET /v1/users/:id

POST /v1/users - body: { email: string, password: string }

PUT /v1/users/:id - body: { email: string, password: string }

DELETE /v1/users/:id

Right routes

GET /v1/rights

GET /v1/rights/:id

POST /v1/rights - body: { name: string, description?: string }

PUT /v1/rights/:id - body: { name: string, description?: string }

DELETE /v1/rights/:id

Role routes

GET /v1/roles

GET /v1/roles/:id

POST /v1/roles - body: { name: string, description?: string, rights?: ObjectId[] }

PUT /v1/roles/:id - body: { name: string, description?: string, rights?: ObjectId[] }

DELETE /v1/roles/:id

Badges
Extracted from project README
GitHub license
Related Projects