├── .gitignore ├── package.json ├── routes └── product.route.js ├── Models └── product.model.js ├── index.js └── controllers └── product.controller.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "curd", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "nodemon index.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "dotenv": "^16.4.5", 15 | "express": "^4.21.0", 16 | "mongodb": "^6.9.0", 17 | "mongoose": "^8.7.0", 18 | "nodemon": "^3.1.7" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /routes/product.route.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | const Product =require("../Models/product.model.js") 4 | const { getProducts, getProduct, createProduct, updateProduct, deleteProduct } = require('../controllers/product.controller.js'); 5 | 6 | // Routes 7 | router.get('/', getProducts); 8 | router.get("/:id", getProduct); 9 | router.post("/", createProduct); 10 | router.put("/:id", updateProduct); 11 | router.delete("/:id", deleteProduct); 12 | 13 | module.exports = router; 14 | -------------------------------------------------------------------------------- /Models/product.model.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const ProductSchema = new mongoose.Schema( 4 | { 5 | name: { 6 | type: String, 7 | required: [true, "Please enter Product name"], 8 | }, 9 | quantity: { 10 | type: Number, 11 | default: 0, 12 | }, 13 | price: { 14 | type: Number, 15 | default: 0, 16 | }, 17 | }, 18 | { 19 | timestamps: true, 20 | } 21 | ); 22 | 23 | const Product = mongoose.model('Product', ProductSchema); 24 | 25 | module.exports = Product; 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const mongoose = require('mongoose'); 4 | const productRoute = require('./routes/product.route.js'); 5 | 6 | // Middleware configuration 7 | app.use(express.json()); 8 | 9 | // Route 10 | app.use("/api/products", productRoute); 11 | 12 | app.get('/', (req, res) => { 13 | res.send('Hello, Express! This is Kaleeswaran'); 14 | }); 15 | 16 | app.listen(5000, () => { 17 | console.log('Server is running on port 5000'); 18 | }); 19 | 20 | // MongoDB Connection 21 | mongoose.connect("mongodb+srv://kalees:Password%402024@crud.y0g05.mongodb.net/?retryWrites=true&w=majority&appName=CRUD") 22 | .then(() => { 23 | console.log("Connected to MongoDB"); 24 | }) 25 | .catch((err) => { 26 | console.error("Connection Failed", err); 27 | }); 28 | -------------------------------------------------------------------------------- /controllers/product.controller.js: -------------------------------------------------------------------------------- 1 | const Product = require('../Models/product.model.js'); 2 | 3 | const getProducts = async (req, res) => { 4 | try { 5 | const products = await Product.find({}); 6 | res.status(200).json(products); 7 | } catch (error) { 8 | res.status(500).json({ message: error.message }); 9 | } 10 | }; 11 | 12 | const getProduct = async (req, res) => { 13 | try { 14 | const { id } = req.params; 15 | const product = await Product.findById(id); 16 | res.status(200).json(product); 17 | } catch (error) { 18 | res.status(500).json({ message: error.message }); 19 | } 20 | }; 21 | 22 | const createProduct = async (req, res) => { 23 | try { 24 | const product = await Product.create(req.body); 25 | res.status(200).json(product); 26 | } catch (error) { 27 | res.status(500).json({ message: error.message }); 28 | } 29 | }; 30 | 31 | const updateProduct = async (req, res) => { 32 | try { 33 | const { id } = req.params; 34 | const product = await Product.findByIdAndUpdate(id, req.body, { new: true }); 35 | 36 | if (!product) { 37 | return res.status(404).json({ message: "Product not found" }); 38 | } 39 | 40 | res.status(200).json(product); 41 | } catch (error) { 42 | res.status(500).json({ message: error.message }); 43 | } 44 | }; 45 | 46 | const deleteProduct = async (req, res) => { 47 | try { 48 | const { id } = req.params; 49 | const product = await Product.findByIdAndDelete(id); 50 | 51 | if (!product) { 52 | return res.status(404).json({ message: "Product not found" }); 53 | } 54 | 55 | res.status(200).json({ message: "Deleted successfully" }); 56 | } catch (error) { 57 | res.status(500).json({ message: error.message }); 58 | } 59 | }; 60 | 61 | module.exports = { 62 | getProducts, 63 | getProduct, 64 | createProduct, 65 | updateProduct, 66 | deleteProduct 67 | }; 68 | --------------------------------------------------------------------------------