REST API for campgrounds application, built with Node.js, Express, MongoDB and Mongoose.
Technology | Purpose |
---|---|
Express | Web framework |
MongoDB | Database |
Mongoose | ODM |
Passport | Authentication |
Express-Session | Session management |
Express-Mongo-Sanitize | Preventing NoSQL injections |
Express-Rate-Limit | Rate limiting |
Cors | Cross-origin resource sharing middleware |
Dotenv | Environment variables |
Helmet | Setting security-related HTTP headers |
Swagger | API documentation |
Zod | Validation Library |
API documentation is available at https://yc-server8080.onrender.com/api-docs
Endpoint | Description |
---|---|
GET /campgrounds |
return campgrounds |
POST /campgrounds |
create campground |
GET /campgrounds/:id |
return campground by id |
PUT /campgrounds/:id |
update campground by id |
DELETE /campgrounds/:id |
delete campground by id |
Endpoint | Description |
---|---|
POST /campgrounds/:cid/reviews |
create review for a campground |
DELETE /campgrounds/:cid/reviews/:rId |
delete review for a campground by id |
Endpoint | Description |
---|---|
POST /register |
register a user |
POST /login |
login a user |
POST /logout |
logout a user |
GET /getUser |
return current user in the session |
const CampgroundSchema = new Schema(
{
title: String,
description: String,
location: String,
price: Number,
image: String,
geometry: {
type: { type: String, enum: ["Point"], required: true },
coordinates: { type: [Number], required: true },
},
author: { type: Schema.Types.ObjectId, ref: "User" },
reviews: [{ type: Schema.Types.ObjectId, ref: "Review" }],
},
{ timestamps: true }
)
const ReviewSchema = new Schema(
{
body: String,
rating: Number,
author: { type: Schema.Types.ObjectId, ref: "User" },
},
{ timestamps: true }
)
const UserSchema = new Schema({
email: String,
username: String,
password: String,
isAdmin: {
type: Boolean,
default: false,
immutable: true,
},
})
npm i
DB_URL= # MongoDB connection string
SECRET= # Secret used for signing session cookie
MAPBOX_TOKEN= # Mapbox API token
CLIENT_ORIGIN= # Client url, used for CORS
# Note: To use a non-https origin,remove the `secure` option from the cookie session
Script | Description |
---|---|
npm run dev |
Run the server in development mode (http://localhost:3000) |
npm run lint |
Lint the code |
npm run format |
Format the code using Prettier |
npm run build |
Build the server for production mode |
npm run clean |
Clean the build directory |
npm start |
Run the server in production mode |
npm run seed |
Seed the database with sample data |
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.