This project is a RESTful API designed to manage a database of cars for sale. It provides endpoints for user authentication and various CRUD operations on the car listings. The API is built with Express.js and uses Mongoose to interact with a MongoDB database. Authentication is handled using JSON Web Tokens (JWT).
- User registration and authentication
- CRUD operations for car listings
- Secure API endpoints using JWT
- Express.js: A minimal and flexible Node.js web application framework.
- Mongoose: An elegant MongoDB object modeling tool for Node.js.
- JWT (JSON Web Tokens): A compact, URL-safe means of representing claims to be transferred between two parties.
- MongoDB: A document-based, distributed database.
- Docker: A platform for developing, shipping, and running applications in containers.
- Node.js
- Docker
- Clone the repository:
git clone https://github.com/yourusername/car-sale-api.git
- Navigate to the project directory:
cd car-sale-api
- Install dependencies:
npm install
- Set up environment variables:
Create a
.env
file in the root directory and add the following:MONGODB_URI=mongodb://localhost:27017/car-sale-db JWT_SECRET=your_jwt_secret
- Pull the official MongoDB image from Docker Hub:
docker pull mongo
- Run a MongoDB container:
docker run --name car-sale-mongo -p 27017:27017 -d mongo
- Verify that the MongoDB container is running:
docker ps
- Start the server:
npm start
The API provides the following endpoints:
POST /api/auth/register
: Register a new userPOST /api/auth/login
: Log in an existing userGET /api/cars
: Get a list of all carsPOST /api/cars
: Add a new carGET /api/cars/:id
: Get details of a specific carPUT /api/cars/:id
: Update a car's informationDELETE /api/cars/:id
: Delete a car