Skip to content

Repository files navigation

Express + Mongoose Production Boilerplate

Production-oriented Node.js API using Express, wired to the schema boilerplate at ../Boilerplate-application-schema (@applicationSchema/schemas — placeholder name).

Quick start

cp .env.example .env
# Set JWT_SECRET and MongoDB values

npm install
npm run start:local

Doppler (optional secrets)

doppler run -- npm run start:local

Docker

cp .env.example .env
docker compose up --build

API base path: /site/v1

Schema boilerplate integration

This repo depends on your schema package via a local file: path:

"@applicationSchema/schemas": "file:../Boilerplate-application-schema"

For a real app, rename the package in the schema repo (e.g. @myAppSchema/schemas) and update package.json + all require() paths here.

The schema package exports:

Export Purpose
initMongo(mongoUri) Connect to MongoDB (throws on failure)
disconnectMongo() Graceful disconnect
isConnected() Connection status check
USER_TYPES Shared user type constants
User Mongoose User model (findByCredentials, findByEmailForPassReset)

Usage in this repo:

// index.js
const { initMongo } = require('@applicationSchema/schemas');
await initMongo(config.mongo.uri);

// server/user/user.helper.js
const { User } = require('@applicationSchema/schemas');

Response format

Success

{
  "success": true,
  "message": "User loaded",
  "data": { "user": {} },
  "meta": { "page": 1, "limit": 10, "total": 42, "pages": 5 }
}

Error

{
  "success": false,
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Invalid credentials."
  }
}

Every response includes an X-Request-Id header for log correlation.

API endpoints

Operational

Method Path Description
GET /health Liveness probe
GET /ready Readiness probe (MongoDB ping)
GET /metrics Prometheus metrics

Public (/site/v1)

Method Path Description
POST /user/register Register (firstName, email, strong password)
POST /user/login Login via User.findByCredentials
POST /user/logout Logout (client discards token)

Protected (Bearer token)

Method Path Description
GET /user-me Current user profile
GET /users List users (Admin/Superuser only)
GET /user/:userID Get user (U-XXXXXXXXXX format)
PUT /user/:userID Update user (owner or admin)
DELETE /user/:userID Delete user (owner or admin)

JWT payload includes user_id, email, and userType (U100 Admin, U200 Superuser, etc.).

Project structure

├── config/             # Config, Express setup, constants
├── server/user/        # Routes → controller → service → helper
├── tests/              # Jest + Supertest (schema mocked in tests)
├── utils/              # Logger, response helpers, metrics
└── index.js            # initMongo + graceful shutdown

Scripts

Script Description
npm start Production
npm run start:local Nodemon dev server
npm test Run tests
npm run lint ESLint

See CONTRIBUTING.md for module conventions.

License

MIT

About

Minimal Boilerplate for personal use

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages