Prerequisites
Node.js v22 or higher is required to run this project. Older versions are not supported.
Installation Steps
Install dependencies
- Express.js for the API server
- Mongoose for MongoDB interactions
- Mocha, Chai, and Supertest for testing
- Faker for mock data generation
Configure environment variables
Create a
.env file in the project root with the required configuration:Start MongoDB
Ensure MongoDB is running locally on port 27017, or update
MONGO_URL to point to your MongoDB instance.Environment Variables
The application usesdotenv to load configuration from a .env file. All environment variables are defined in src/config/config.js:
Configuration Reference
| Variable | Default | Required | Description |
|---|---|---|---|
PORT | 8080 | No | Port number for the Express server |
MONGO_URL | mongodb://127.0.0.1:27017 | Yes | MongoDB connection URL |
DB_NAME | adoptme | Yes | MongoDB database name |
Example Configurations
NPM Scripts
The project includes several npm scripts for different use cases:Available Commands
| Command | Description | Use Case |
|---|---|---|
npm start | Start the production server | Production deployment |
npm run dev | Start development server with nodemon | Local development |
npm test | Run test suite with Mocha | Testing and CI/CD |
Use npm run dev during development. Nodemon automatically restarts the server when you save file changes.
Development vs Production
Development Mode
- Auto-restart on file changes (nodemon)
- Detailed error messages
- Local MongoDB connection
- Verbose logging
Production Mode
- Single process (no auto-restart)
- Optimized error handling
- Remote MongoDB connection (MongoDB Atlas)
- Production logging
MongoDB Setup
Local MongoDB
- Install MongoDB Community Edition
- Start the MongoDB service:
- Verify it’s running:
MongoDB Atlas (Cloud)
- Create a free cluster at mongodb.com/atlas
- Add your IP address to the allowlist
- Create a database user
- Copy the connection string:
Debugging Tips
Server Won’t Start
Check port availability:PORT in .env
Database Connection Errors
Error:MongoServerError: connect ECONNREFUSED
Solutions:
- Verify MongoDB is running
- Check
MONGO_URLin.env - Test connection with mongosh:
Environment Variables Not Loading
Error: Application uses default values instead of.env values
Solutions:
- Ensure
.envis in the project root - Check file name (exactly
.env, notenv.txtor.env.local) - Restart the server after modifying
.env
Test Failures
Error: Tests timeout or fail to connect Solutions:- Ensure MongoDB is running
- Check that the database is accessible
- Verify
DB_NAMEin.env - Run tests with increased timeout:
Docker Development
Building the Image
Running with Docker
When running in Docker, use
host.docker.internal instead of localhost to access the host machine’s MongoDB.Docker Compose
Create adocker-compose.yml for a complete development stack:
Project Structure
IDE Setup
VS Code Recommended Extensions
- ESLint: Linting and code quality
- Prettier: Code formatting
- MongoDB for VS Code: Database exploration
- REST Client: Test API endpoints
- Thunder Client: Alternative API client
VS Code Settings
Create.vscode/settings.json:
Common Development Workflows
Making Code Changes
- Start development server:
npm run dev - Make changes to source files
- Nodemon automatically restarts the server
- Test changes with REST client or browser
Adding New Endpoints
- Create/modify router in
src/routes/ - Add tests in
test/supertest.test.js - Run tests:
npm test - Document in API reference
Testing Locally
- Ensure MongoDB is running
- Run test suite:
npm test - Check test output for failures
- Fix issues and re-run tests
Next Steps
- Write automated tests for your changes
- Generate mock data for development
- Explore the API Reference to understand available endpoints
- Set up CI/CD for automated testing and deployment