Changeset 717ceae for backend/server.js
- Timestamp:
- 08/30/22 14:13:43 (2 years ago)
- Branches:
- master
- Children:
- 16237c4
- Parents:
- b612ab1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/server.js
rb612ab1 r717ceae 1 1 import express from "express"; 2 2 import data from "./data.js"; 3 import mongoose from "mongoose"; 4 import dotenv from "dotenv"; 5 import seedRouter from "./routes/seedRoutes.js"; 6 import productRouter from "./routes/productRoutes.js"; 7 8 dotenv.config(); 9 10 mongoose 11 .connect(process.env.MONGODB_URI) 12 .then(() => { 13 console.log("connected to db"); 14 }) 15 .catch((err) => { 16 console.log(err.message); 17 }); 3 18 4 19 const app = express(); 5 6 app.get("/api/products", (req, res) => { 7 res.send(data.products); 8 }); 9 10 app.get("/api/products/slug/:slug", (req, res) => { 11 const product = data.products.find((x) => x.slug === req.params.slug); 12 if (product) { 13 res.send(product); 14 } else { 15 res.status(404).send({ message: "Продуктот не е пронајден" }); 16 } 17 }); 18 19 app.get("/api/products/:id", (req, res) => { 20 const product = data.products.find((x) => x._id === req.params.id); 21 if (product) { 22 res.send(product); 23 } else { 24 res.status(404).send({ message: "Продуктот не е пронајден" }); 25 } 26 }); 20 app.use("/api/seed", seedRouter); 21 app.use("/api/products", productRouter); 27 22 28 23 const port = process.env.PORT || 5000;
Note:
See TracChangeset
for help on using the changeset viewer.