Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aman #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Aman #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));