π©π» λ°±μλ(Back-End)/Node js
[E-Commerce App with REST API] (17) λΈλλ μ‘°ννκΈ° (GET) & λͺ¨λ λΈλλ μ‘°ννκΈ° (GET)
μ§μ§μνμΉ΄
2023. 3. 31. 11:54
728x90
λ°μν
<λ³Έ λΈλ‘κ·Έλ Developers Corner μ μ νλΈλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€ :-)>
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example!
π· λΈλλ μ‘°ννκΈ° (GET)
π· λͺ¨λ λΈλλ μ‘°ννκΈ° (GET)
π· μ½λ
β controllers/brandCtrl.js
const Brand = require("../models/Brand");
const asyncHandler = require("express-async-handler");
const { validateMongodbID } = require("../utils/validateMongodbID");
// λΈλλ μμ±νκΈ°
const createBrand = asyncHandler(async (req, res) => {
try {
const newBrand = await Brand.create(req.body);
res.json(newBrand);
}
catch (error) {
throw new Error(error);
}
});
// λΈλλ μμ νκΈ°
const updateBrand = asyncHandler(async (req, res) => {
const { id } = req.params;
validateMongodbID(id);
try {
const updateBrand = await Brand.findByIdAndUpdate(id, req.body, {
new: true,
});
res.json(updateBrand);
}
catch (error) {
throw new Error(error);
}
});
// λΈλλ μμ νκΈ°
const deleteBrand = asyncHandler(async (req, res) => {
const { id } = req.params;
validateMongodbID(id);
try {
const deleteBrand = await Brand.findByIdAndDelete(id);
res.json(deleteBrand);
}
catch (error) {
throw new Error(error);
}
});
// λΈλλ μ‘°ννκΈ°
const getaBrand = asyncHandler(async (req, res) => {
const { id } = req.params;
validateMongodbID(id);
try {
const getaBrand = await Brand.findById(id);
res.json(getaBrand);
}
catch (error) {
throw new Error(error);
}
});
// λͺ¨λ λΈλλ μ‘°ννκΈ°
const getallBrand = asyncHandler(async (req, res) => {
try {
const getallBrand = await Brand.find();
res.json(getallBrand);
}
catch (error) {
throw new Error(error);
}
});
module.exports = {
createBrand,
updateBrand,
deleteBrand,
getaBrand,
getallBrand,
}
β models/Brand.js
const mongoose = require("mongoose");
const BrandSchema = new mongoose.Schema({
title: {
type: String,
required: true,
unique: true,
index: true
},
}, {
timestamps: true,
collection: 'brand'
});
const Brand = mongoose.model("Brand", BrandSchema);
module.exports = Brand;
β routes/brandRoute.js
const express = require("express");
const { createBrand, updateBrand, deleteBrand, getaBrand, getallBrand } = require("../controllers/brandCtrl");
const { authMiddleware, isAdmin } = require("../middlewares/authMiddleware");
const router = express.Router();
router.post("/", authMiddleware, isAdmin, createBrand);
router.put("/:id", authMiddleware, isAdmin, updateBrand);
router.delete("/:id", authMiddleware, isAdmin, deleteBrand);
router.get("/:id", getaBrand);
router.get("/", getallBrand);
module.exports = router;
β index.js
const express = require("express");
const bodyParser = require("body-parser");
const dbConnect = require("./config/dbConnect");
const {notFound, errorHandler} = require("./middlewares/errorHandler");
const app = express();
require("dotenv").config();
const PORT = process.env.PORT || 8000;
const authRouter = require("./routes/authRoute");
const productRouter = require("./routes/productRoute");
const blogRouter = require("./routes/blogRoute");
const prodCategoryRouter = require("./routes/prodCategoryRoute");
const blogCategoryRouter = require("./routes/blogCategoryRoute");
const brandRouter = require("./routes/brandRoute");
const cookieParser = require("cookie-parser");
// mongoDB
dbConnect();
// Bodyparser
// expressμλ²λ‘ POSTμμ²μ ν λ inputνκ·Έμ valueλ₯Ό μ λ¬
// URL-encoded νμμ λ¬Έμμ΄λ‘ λμ΄μ€κΈ° λλ¬Έμ κ°μ²΄λ‘μ λ³ν νμ
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use("/api/user", authRouter);
app.use("/api/product", productRouter);
app.use("/api/blog", blogRouter);
app.use("/api/prodCategory", prodCategoryRouter);
app.use("/api/blogCategory", blogCategoryRouter);
app.use("/api/brand", brandRouter);
app.use(notFound);
app.use(errorHandler);
app.use("/", (req, res) => {
res.send("hihi");
})
app.listen(PORT, () => {
console.log(`π Server started on port http://localhost:${PORT}`);
});
728x90
λ°μν