This repository contains the backend code for an e-commerce website's API built with Node.js, Express.js, and MongoDB. The API provides endpoints for users to perform CRUD (Create, Read, Update, Delete) operations on products and orders.
To get started with the project, follow the instructions below:
- Clone the repository:
git clone https://github.com/gupta123shivam/E-Commerce-API.git
- Install the dependencies:
npm install
- Create a
.env
file in the root directory of the project and add the required environment variables:
PORT=<port number>
MONGO_URI=<MongoDB connection string>
JWT_SECRET=<secret key for JWT>
- Start the development server:
npm run dev
- Use an API testing tool such as Postman or Insomnia to test the endpoints.
The following endpoints are available in the API:
/api/products
:GET
: get all productsPOST
: create a new product/api/products/:id
:GET
: get a single product by IDPUT
: update a product by IDDELETE
: delete a product by ID/api/orders
:GET
: get all ordersPOST
: create a new order/api/orders/:id
:GET
: get a single order by IDPUT
: update an order by IDDELETE
: delete an order by ID
The API uses JSON Web Tokens (JWT) for authentication. To access the protected endpoints, you must include a valid JWT in the Authorization header of your request. To obtain a JWT, send a POST
request to the /api/users/login
endpoint with a valid username and password. The response will include a JWT that you can use to access the protected endpoints.
The API uses custom error handlers to provide informative error messages in case of errors. If an error occurs, the API will return a JSON response with a descriptive error message.
Contributions to the project are welcome. To contribute, fork the repository and create a pull request with your changes. Before submitting a pull request, make sure that your changes pass the linting and testing checks.
This project is licensed under the MIT License - see the LICENSE.md
file for details.