FreshHarvest API serves as the backend for the FreshHarvest platform, providing essential functionalities for managing users, products, carts, orders, and frequently asked questions (FAQs). This API is built using Node.js and Express.js, with MongoDB as the database. It is written in TypeScript, offering type safety and better code organization.
- User Authentication: Allows users to login and register securely.
- User Management: Enables users to update their profiles, change passwords, and delete accounts.
- Product Management: Provides CRUD operations for managing products.
- Cart Management: Supports adding/removing items from the cart and viewing cart summary.
- Order Management: Facilitates order creation and retrieval for users.
- FAQ Management: Allows administrators to manage frequently asked questions.
I have deployed the API on Render, a cloud platform for hosting and scaling web applications and static sites. This is a free account so the API may not be available all the time. You will need to wait for a few seconds for the server to wake up. The API is deployed and accessible at https://freshharvest-api.onrender.com/api.
To run the API locally, follow these steps:
-
Clone the repository:
git clone https://github.com/sarojadhikari076/freshharvest-api.git
-
Install dependencies:
npm install
-
Set up environment variables:
Create a
.env
file in the root directory and add the following variables:PORT=3000 MONGODB_URI='mongodb://localhost:27017/freshharvest' JWT_SECRET=<your-jwt-secret>
-
Run the development server:
npm run start:dev
-
Seed the database:
To seed the database with sample data, run the following command:
npm run seed
POST /auth/login
: User loginPOST /auth/register
: User registration
GET /auth/me
: Get current user profilePATCH /auth/me
: Update current user profilePATCH /auth/me/password
: Update current user passwordDELETE /auth/me
: Delete current user account
GET /carts
: Get current user's cartPOST /carts
: Add a product to the cartDELETE /carts
: Remove a product from the cartGET /carts/summary
: Get summary of the cart
GET /orders
: Get current user's ordersPOST /orders
: Create a new order
GET /products
: Get all productsGET /products/featured
: Get featured productsGET /products/best-selling
: Get best selling productsGET /products/sub-categories
: Get sub-categories of productsGET /products/:slug
: Get product by slug
GET /faqs
: Get all FAQsGET /faqs/:id
: Get FAQ by IDPOST /faqs
: Create a new FAQPUT /faqs/:id
: Update an existing FAQDELETE /faqs/:id
: Delete an existing FAQ
- Node.js: A JavaScript runtime for building server-side applications.
- Express.js: A minimalist web framework for Node.js, used for handling HTTP requests and routes.
- MongoDB: A NoSQL database used for storing application data.
- TypeScript: A statically typed superset of JavaScript that compiles to plain JavaScript. It provides type safety, enhanced code quality, and better tooling support.
- Mongoose: An Object Data Modeling (ODM) library for MongoDB and Node.js, used for modeling application data and interacting with MongoDB.
- JWT (JSON Web Tokens): A compact, URL-safe means of representing claims to be transferred between two parties. JWTs are used for user authentication and authorization.
- bcrypt: A library for hashing passwords securely, used for user password encryption.
- dotenv: A zero-dependency module for loading environment variables from a
.env
file. - nodemon: A utility that monitors for changes in files and automatically restarts the server.
- rimraf: A cross-platform tool for removing directories and their contents.
- ts-node: A TypeScript execution and REPL for Node.js, used for running TypeScript code directly without compilation.
- TypeScript Compiler (tsc): The TypeScript compiler that converts TypeScript code into JavaScript.
This project is licensed under the ISC License.
- Saroj Adhikari(23221436)
- GitHub: Saroj Adhikari