A modern, production-ready Node.js REST API built with Express.js featuring configuration management, input validation, and comprehensive testing. This project serves as a template and learning resource for building scalable backend services.
Built in 2021. This application demonstrates best practices for building REST APIs with Node.js, including proper error handling, validation, configuration management, and testing.
- 🚀 Express.js REST API server
- ✅ Input validation using Joi
- 🔧 Environment-based configuration management
- 🧪 Comprehensive test suite with Mocha & Chai
- 📝 ESLint code quality enforcement (Airbnb style)
- 🔄 Hot-reload development with nodemon
- 🏗️ Clean, modular architecture
- 📦 Mock S3 service integration
- 🔒 Validation middleware
- 🌍 Multi-environment support (development, test, production)
- Express.js REST API: High-performance web server setup
- Input Validation: Joi-powered request body validation
- Environment Management: Robust configuration using
configmodule - Comprehensive Testing: Full coverage with Mocha and Chai
- S3 Service Mock: Integration-ready service architecture
- Modular Architecture: Clear separation between routes, controllers, and services
- Error Handling: Proper HTTP status codes and structured responses
- Code Quality: Strict linting with Airbnb style guide
- Hot-Reload: Efficient development workflow with nodemon
- Documentation: Self-documenting API structure and Mermaid diagrams
- Easy Setup: Zero-config development start
- Testing Framework: Ready-to-use testing helpers
- Linting: Pre-configured ESLint for consistent code style
- Environment Overrides: Simple configuration for dev/test/prod
- Clear Flow: Visualized request/response flow with Mermaid
- Node.js (v12 or higher)
- npm or pnpm
- Clone the repository:
git clone https://github.com/orassayag/modern-2021-node-api.git
cd modern-2021-node-api- Install dependencies:
npm install- Start the development server:
npm run devThe server will start on http://localhost:4001
Configuration is managed using the config module. Edit configuration files in the config/ directory:
config/default.js- Default settingsconfig/development.js- Development overridesconfig/production.js- Production overridesconfig/test.js- Test environment overrides
Environment Variables:
PORT- Server port (default: 4001)HOST- Server host (default: localhost)AWS_URL- AWS S3 URL (default: https://s3.amazon.com)NODE_ENV- Environment: development, test, or production
npm run devnpm testnpm startRuns the server with automatic reload on file changes:
npm run devRuns the server in production mode:
npm startRuns the test suite:
npm testChecks code quality:
npm run lintRetrieves the current configuration.
Response:
{
"a": "value",
"b": "value"
}Status: 200 OK
POST /veryhiddenapi/config/
Creates or updates configuration (protected endpoint).
Request Body:
{
"a": "string",
"b": "string"
}Status: 201 Created
Validation: Request body is validated against Joi schema.
modern-2021-node-api/
├── src/
│ ├── index.js # Application entry point
│ ├── httpServer/
│ │ ├── index.js # Express server setup
│ │ ├── controllers/ # Request handlers
│ │ │ └── configController.js
│ │ ├── middleware/ # Validation middleware
│ │ │ ├── configSchema.js
│ │ │ └── validate.js
│ │ └── routes/ # API routes
│ │ ├── index.js
│ │ └── configRoutes.js
│ └── services/ # Business logic services
│ └── s3Service.js
├── config/ # Environment configuration
│ ├── default.js
│ ├── development.js
│ ├── production.js
│ └── test.js
├── test/ # Test files
│ ├── helper/
│ │ └── test_helper.js
│ └── controllers/
│ └── config.spec.js
├── .eslintrc.js # ESLint configuration
├── package.json
└── README.md
The project follows a modular structure organized by responsibility:
src/: Core application logichttpServer/: Express server, routes, and controllersservices/: Business logic and external integrations
config/: Environment-based configuration filestest/: Comprehensive test suitelogs/: Application logs (created at runtime)
graph TD
A[Client] -->|HTTP Request| B[Express Server]
B --> C{Route Handler}
C -->|/api/config/| D[Config Routes]
C -->|/veryhiddenapi/config/| E[Protected Config Routes]
D --> F[Validation Middleware]
E --> F
F -->|Valid| G[Config Controller]
F -->|Invalid| H[Error Response]
G --> I[S3 Service]
I --> J[AWS S3]
G -->|Success| K[JSON Response]
G -->|Error| H
K --> A
H --> A
subgraph "Configuration Layer"
L[Config Module]
M[Environment Variables]
L --> M
end
subgraph "Server Layer"
B
C
end
subgraph "Business Logic"
G
I
end
L -.->|Provides Config| B
sequenceDiagram
participant Client
participant Express
participant Validation
participant Controller
participant Service
participant S3
Client->>Express: POST /veryhiddenapi/config/
Express->>Validation: Validate Request Body
alt Invalid Input
Validation->>Client: 400 Bad Request
else Valid Input
Validation->>Controller: Process Request
Controller->>Service: uploadConfig(data)
Service->>S3: Upload to S3
S3-->>Service: Success
Service-->>Controller: Success
Controller->>Client: 201 Created
end
The project follows modern JavaScript best practices:
- ES6+ syntax with async/await
- Modular architecture with clear separation of concerns
- Input validation using Joi schemas
- Error handling with proper HTTP status codes
- Testing with Mocha and Chai
- Code quality enforced by ESLint (Airbnb configuration)
- Create route definition in
src/httpServer/routes/ - Add controller logic in
src/httpServer/controllers/ - Create validation schema in
src/httpServer/middleware/ - Write tests in
test/controllers/ - Register routes in
src/httpServer/routes/index.js
- Separation of Concerns: Controllers handle requests, services handle logic
- Configuration-Driven: All environment variables and settings managed centrally
- Fail-Fast Validation: Request validation happens at the entry point
- Test-First Mentality: Built-in testing helpers and specs
- Standardized Responses: Consistent JSON response formats
- MVC (Model-View-Controller): Simplified for REST API (Controller/Service)
- Middleware Pattern: For validation and error handling
- Singleton Pattern: For configuration and service instances
- Dependency Injection: Simple manual injection in routes
The project uses:
- Mocha - Test framework
- Chai - Assertion library
- chai-http - HTTP integration testing
Tests cover:
- API endpoint functionality
- Request validation
- Success and error scenarios
- HTTP status codes
Run tests:
npm test- Environment Separation: Always use the correct
NODE_ENVfor your operations - Input Validation: Define Joi schemas for all new endpoints
- Modular Services: Keep business logic in services, not controllers
- Test Coverage: Write tests for both success and error scenarios
- Linting: Ensure all code passes ESLint before committing
Contributions to this project are released to the public under the project's open source license.
Everyone is welcome to contribute. Please see CONTRIBUTING.md for detailed guidelines.
For questions, issues, or contributions:
- GitHub Issues: https://github.com/orassayag/modern-2021-node-api/issues
- Email: orassayag@gmail.com
- Or Assayag - Initial work - orassayag
- Or Assayag orassayag@gmail.com
- GitHub: https://github.com/orassayag
- StackOverflow: https://stackoverflow.com/users/4442606/or-assayag?tab=profile
- LinkedIn: https://linkedin.com/in/orassayag
This application has an MIT license - see the LICENSE file for details.
- Built for educational and research purposes
- Respects robots.txt and implements rate limiting
- Uses user-agent rotation to avoid detection
- Implements polite crawling practices