Changeset 717ceae for backend/server.js


Ignore:
Timestamp:
08/30/22 14:13:43 (2 years ago)
Author:
Nace Gjorgjievski <nace.gorgievski123@…>
Branches:
master
Children:
16237c4
Parents:
b612ab1
Message:

connected db

File:
1 edited

Legend:

Unmodified
Added
Removed
  • backend/server.js

    rb612ab1 r717ceae  
    11import express from "express";
    22import data from "./data.js";
     3import mongoose from "mongoose";
     4import dotenv from "dotenv";
     5import seedRouter from "./routes/seedRoutes.js";
     6import productRouter from "./routes/productRoutes.js";
     7
     8dotenv.config();
     9
     10mongoose
     11  .connect(process.env.MONGODB_URI)
     12  .then(() => {
     13    console.log("connected to db");
     14  })
     15  .catch((err) => {
     16    console.log(err.message);
     17  });
    318
    419const 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 });
     20app.use("/api/seed", seedRouter);
     21app.use("/api/products", productRouter);
    2722
    2823const port = process.env.PORT || 5000;
Note: See TracChangeset for help on using the changeset viewer.