This project is a complete backend API for a product management application, built with the NestJS framework. It provides all the necessary endpoints for CRUD (Create, Read, Update, Delete) operations on products and is configured for deployment on Vercel.
- Full CRUD Functionality: Endpoints for creating, reading, updating, and deleting products.
- Database Integration: Uses MongoDB with Mongoose and Typegoose for data modeling and persistence.
- Validation: Implements
class-validatorandclass-transformerfor robust request data validation. - CORS Enabled: Configured to allow requests from a specific frontend application URL.
- Ready for Deployment: Includes a
vercel.jsonfile for easy deployment to Vercel.
- NestJS: A progressive Node.js framework for building efficient and scalable server-side applications.
- MongoDB: A NoSQL database for storing product data.
- Mongoose: An elegant MongoDB object modeling tool for Node.js.
- Typegoose: A library for creating Mongoose models with TypeScript classes.
- TypeScript: A typed superset of JavaScript that compiles to plain JavaScript.
- Vercel: A cloud platform for static sites and Serverless Functions.
The project follows the standard NestJS project structure:
src/
├── app.controller.spec.ts # Unit test for the main controller
├── app.controller.ts # Main application controller
├── app.module.ts # Main application module
├── app.service.ts # Main application service
├── main.ts # Application entry file
└── products/ # Products module
├── dto/ # Data Transfer Objects for products
│ ├── create-product.dto.ts
│ └── update-product.dto.ts
├── models/ # Data models (Mongoose Schema)
│ └── product.model.ts
├── products.controller.ts # Controller for product routes
├── products.module.ts # Module that encapsulates product logic
└── products.service.ts # Service with business logic for products
This project uses MongoDB as its database. The connection is managed through the MongooseModule in app.module.ts. Product data is structured according to the schema defined in src/products/schemas/product.schema.ts using Typegoose.
The following endpoints are available for the products resource. The base URL is /products.
| Method | Endpoint | Description |
|---|---|---|
GET |
/products |
Get a list of all products. |
GET |
/:id |
Get a single product by ID. |
POST |
/products |
Create a new product. |
PATCH |
/:id |
Update an existing product. |
DELETE |
/:id |
Delete a product by ID. |
To run this project, you need to create a .env file in the root directory with the following variables:
DATABASE_URL: The connection string for your MongoDB database.JWT_SECRET: A secret key for JWT token generation (if you add authentication).CORS_ORIGIN: The URL of the frontend application that will be allowed to make requests (e.g.,http://localhost:3000).
-
Clone the repository:
git clone https://github.com/your-username/nestcrud.git cd nestcrud -
Install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in the root and add the variables mentioned above. -
Run the application:
# Development mode with watch npm run start:devThe application will be running on
http://localhost:3000.
This project is configured for deployment on Vercel. The vercel.json file ensures that the NestJS application is correctly built and served as a serverless function.
To deploy, simply connect your GitHub repository to Vercel and let it build and deploy automatically. Remember to set the environment variables in the Vercel project settings.