A minimal seed project for building RESTful APIs with Express.js and MongoDB using Mongoose ORM.
- Express.js server with ES6 modules
- MongoDB integration with Mongoose ORM
- Layered architecture (Controllers, Services, Models)
- Authentication middleware with external OAuth service
- Request logging middleware
- RESTful API structure with protected and public routes
- Mongoose schema with timestamps
- Swagger API documentation with organized tags
- Jest testing setup with MongoDB Memory Server
- Axios for HTTP requests
- Prettier code formatting
- Hot reload development with --watch flag
- Node.js 22+
- MongoDB instance (local or remote)
-
Clone the repository
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory:NODE_ENV=development MONGO_URI=mongodb://localhost:27017/express-seed PORT=6091 AUTH_CLIENT_ID="" AUTH_ENDPOINT="" AUTH=""
-
Start the development server:
npm start
-
Access the API documentation at: http://localhost:6091/api-docs
├── src/
│ ├── controllers/ # Route controllers (handle HTTP requests/responses)
│ ├── services/ # Business logic layer and external API calls
│ ├── models/ # Mongoose schemas and models
│ ├── routes/ # API routes
│ │ ├── middlewares/ # Route-specific middlewares (auth, etc.)
│ │ └── public-api/ # Public API routes (no authentication)
│ ├── middlewares/ # Global middlewares (logging, etc.)
│ ├── helpers/ # Helper functions (database, testing)
│ └── docs/ # Swagger documentation
├── tests/ # Test files (models, services, controllers, routes)
├── index.js # Application entry point
└── package.json
npm start
- Start the development server with hot reloadnpm test
- Run testsnpm run test:watch
- Run tests in watch modenpm run format
- Format code with Prettier
All /api/*
endpoints require an Authorization
header with a valid bearer token.
GET /api/examples
- Get all examplesGET /api/examples/:id
- Get example by IDPOST /api/examples
- Create a new examplePUT /api/examples/:id
- Update an exampleDELETE /api/examples/:id
- Delete an example
POST /public-api/login
- User authenticationPOST /public-api/session
- Validate authentication token
GET /api-docs
- Swagger UI documentation
The API uses an external OAuth service for authentication. Protected routes require a valid Bearer token in the Authorization header:
Authorization: Bearer <your-token>
All requests are logged with the following format:
[2025-01-21T15:30:45.123Z] GET /api/examples - Request received
[2025-01-21T15:30:45.156Z] GET /api/examples - 200 (33ms)
The project includes comprehensive tests for all layers:
- Model tests - Test Mongoose schemas and database operations
- Service tests - Test business logic
- Controller tests - Test HTTP handling with mocked services
- Route tests - Integration tests for complete API endpoints
Tests are configured with Jest and MongoDB Memory Server for isolated testing:
npm test
The project follows a layered architecture:
- Routes - Handle HTTP routing and middleware application
- Controllers - Handle HTTP requests/responses and validation
- Services - Contain business logic and external API calls
- Models - Define data structures and database operations
- express - Web framework
- mongoose - MongoDB ODM
- axios - HTTP client for external API calls
- swagger-ui-express - API documentation
- jest - Testing framework
- mongodb-memory-server - In-memory MongoDB for tests
- prettier - Code formatting
- supertest - HTTP testing
ISC