๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[E-Commerce App with REST API] (17) ๋ธ๋๋ ์กฐํํ๊ธฐ (GET) & ๋ชจ๋ ๋ธ๋๋ ์กฐํํ๊ธฐ (GET) ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
[E-Commerce App with REST API] (17) ๋ธ๋๋ ์กฐํํ๊ธฐ (GET) & ๋ชจ๋ ๋ธ๋๋ ์กฐํํ๊ธฐ (GET)
์ง์ง์ํ์นด 2023. 3. 31. 11:54728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ 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
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments