Skip to content

Commit

Permalink
Merge pull request #1 from Ambesawi/aman
Browse files Browse the repository at this point in the history
Aman
  • Loading branch information
Ambesawi authored Jan 31, 2024
2 parents 789d18c + 67a16b8 commit 01386e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: node server.js
web: node server.js
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

19 changes: 18 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -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",
{
Expand All @@ -18,6 +22,7 @@ mongoose.connect(
}
);

// Define the Product model schema
const Product = mongoose.model(
"products",
new mongoose.Schema({
Expand All @@ -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(
Expand Down Expand Up @@ -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 ||
Expand All @@ -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"));

0 comments on commit 01386e8

Please sign in to comment.