A ready-to-use Express + GraphQL server boilerplate with a clean, layered structure and switchable MongoDB / MySQL persistence.
- Layered REST API (
Route → Controller → Service → Repository → Model). - Dual ORM: MongoDB (Mongoose) or MySQL (Sequelize), selected by
DB_TYPE— one active per run. - JWT authentication (
jsonwebtoken) with bcrypt password hashing. - Request validation with Joi (and a generic Ajv validator middleware).
- Security & resilience middleware: helmet, cors, rate limiting, too-busy guard.
- Logging with winston; i18n via
i18n-express(translations onreq.app.locals.trans). - Response compression and optional HTTPS.
- Socket.io support (http & https). Note: not cluster-safe — needs a redis socket adapter.
- GraphQL wired with Apollo (resolvers are placeholder examples for clarification).
- Fail-fast environment validation at boot.
npm icp .env.example .env, then follow the comments in.env.npm run secret— generatesAPP_SECRETused for hashing/signing.- Pick your database in
.envviaDB_TYPE(mongodbormysql). npm run start(ornpm run devfor nodemon).
The compose stack runs the app alongside both MongoDB and MySQL, so DB_TYPE
works either way without editing compose files.
cp .env.example .envand set:DB_TYPE=mongodb→DB_HOST=mongo,DB_PORT=27017, orDB_TYPE=mysql→DB_HOST=mysql,DB_PORT=3306- set
DB_NAME, and (for MySQL)DB_USERNAME/DB_PASSWORD.
docker compose up --build- The API is available on
http://localhost:${PORT}(default 3000).
npm run dev— start with nodemon (development).npm run start— start the server.npm test— run the Jest test suite.npm run lint— run ESLint.npm run secret— generate and writeAPP_SECRETinto.env.
REST examples live in api/v1/ (e.g. user registration at POST {SERVER_ROOT_URL}/users/create).
GraphQL is served at /graphql.