A RESTful API for managing camping locations with image upload functionality.
- ✅ User authentication (signup/login) with JWT
- ✅ CRUD operations for camps
- ✅ Image upload functionality
- ✅ Swagger API documentation
- ✅ MongoDB integration
- ✅ File upload support
- Node.js (v14 or higher)
- MongoDB (local or cloud)
- npm or yarn
-
Clone the repository
-
Install dependencies:
npm install
-
Create a
.envfile in the root directory with the following variables:MONGO_URI=mongodb://localhost:27017/camp-explorer JWT_SECRET=your-super-secret-jwt-key-change-this-in-production PORT=5000
-
Start the development server:
npm run dev
Once the server is running, visit http://localhost:5000/api-docs to access the Swagger documentation.
POST /api/auth/signup- Register a new userPOST /api/auth/login- Login user
GET /api/camps- Get all campsGET /api/camps/:id- Get camp by IDPOST /api/camps- Create a new camp (with image upload)PUT /api/camps/:id- Update camp (with image upload)DELETE /api/camps/:id- Delete campGET /api/camps/:id/image- Get camp image
The API supports image uploads for camps. Images are stored as buffers in the database for better deployment compatibility.
Features:
- Images stored as binary data in MongoDB
- No file system dependencies
- Works on all cloud platforms
- Automatic file type validation
- 5MB file size limit
Supported formats: JPG, JPEG, PNG
How to use images:
- Upload image with camp creation/update
- Get image URL:
GET /api/camps/:id/image - Frontend can display:
<img src="/api/camps/:id/image" />
- Start the server:
npm run dev - Open
http://localhost:5000/api-docs - You can test all endpoints directly from the Swagger UI
- For image uploads, use the file upload feature in Swagger
server/
├── config/
│ └── db.js # Database configuration
├── controllers/
│ └── campController.js # Camp business logic
├── middlewares/
│ └── upload.js # File upload middleware
├── models/
│ ├── Camp.js # Camp model
│ └── User.js # User model
├── routes/
│ ├── auth.js # Authentication routes
│ └── campRoutes.js # Camp routes
├── uploads/ # (No longer used - images stored in database)
├── index.js # Main server file
└── package.json
| Variable | Description | Default |
|---|---|---|
MONGO_URI |
MongoDB connection string | mongodb://localhost:27017/camp-explorer |
JWT_SECRET |
Secret key for JWT tokens | Required |
PORT |
Server port | 5000 |
express- Web frameworkmongoose- MongoDB ODMbcryptjs- Password hashingjsonwebtoken- JWT authenticationmulter- File upload handlingcors- Cross-origin resource sharingdotenv- Environment variablesswagger-jsdoc- API documentationswagger-ui-express- Swagger UI
nodemon- Development server with auto-restart