diff --git a/Procfile b/Procfile index 6f86b16..489b270 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web: node server.js \ No newline at end of file +web: node server.js diff --git a/README.md b/README.md index 4532697..d343349 100644 --- a/README.md +++ b/README.md @@ -383,3 +383,4 @@ Demo: https://reactredux-shoppingcart.herokuapp.com/ 37. app.get("/", (req, res) => res.sendFile(\_\_dirname + "/build/index.html")); 38. git add . && git commit -m "publish" 39. git push heroku + diff --git a/server.js b/server.js index 256216c..16604a1 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,18 @@ +// Import necessary libraries const express = require("express"); const bodyParser = require("body-parser"); const mongoose = require("mongoose"); const shortid = require("shortid"); +// Create an instance of the Express application const app = express(); app.use(bodyParser.json()); +// Serve static files from the "build" directory app.use("/", express.static(__dirname + "/build")); app.get("/", (req, res) => res.sendFile(__dirname + "/build/index.html")); +// Connect to MongoDB using Mongoose mongoose.connect( process.env.MONGODB_URL || "mongodb://localhost/react-shopping-cart-db", { @@ -18,6 +22,7 @@ mongoose.connect( } ); +// Define the Product model schema const Product = mongoose.model( "products", new mongoose.Schema({ @@ -30,22 +35,27 @@ const Product = mongoose.model( }) ); +// Define routes for handling product-related operations app.get("/api/products", async (req, res) => { + // Retrieve all products from the database const products = await Product.find({}); res.send(products); }); app.post("/api/products", async (req, res) => { + // Create and save a new product based on the request body const newProduct = new Product(req.body); const savedProduct = await newProduct.save(); res.send(savedProduct); }); app.delete("/api/products/:id", async (req, res) => { + // Delete a product based on its ID const deletedProduct = await Product.findByIdAndDelete(req.params.id); res.send(deletedProduct); }); +// Define the Order model schema const Order = mongoose.model( "order", new mongoose.Schema( @@ -73,7 +83,9 @@ const Order = mongoose.model( ) ); +// Define routes for handling order-related operations app.post("/api/orders", async (req, res) => { + // Validate and save a new order based on the request body if ( !req.body.name || !req.body.email || @@ -86,14 +98,19 @@ app.post("/api/orders", async (req, res) => { const order = await Order(req.body).save(); res.send(order); }); + app.get("/api/orders", async (req, res) => { + // Retrieve all orders from the database const orders = await Order.find({}); res.send(orders); }); + app.delete("/api/orders/:id", async (req, res) => { + // Delete an order based on its ID const order = await Order.findByIdAndDelete(req.params.id); res.send(order); }); +// Set up the server to listen on the specified port const port = process.env.PORT || 5000; -app.listen(port, () => console.log("serve at http://localhost:5000")); +app.listen(port, () => console.log("Server is running at http://localhost:5000"));