Milehigh-wrld / Milehigh.world

Milehigh.world is a dynamic and inclusive platform that celebrates the art of music, connects musicians worldwide, and provides a supportive environment for learning, inspiration, and collaboration. Whether you're a singer, songwriter, instrumentalist, producer, or music lover, it offers a space to share your passion, grow your skills, and network
https://www.milehigh.world
MIT License
5 stars 11 forks source link

Server.js #29

Closed Cirruslucent closed 4 months ago

Cirruslucent commented 4 months ago

import express from "express"; import fetch from "node-fetch"; import "dotenv/config"; import path from "path";

const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PORT = 8888 } = process.env; const base = "https://api-m.sandbox.paypal.com"; const app = express();

app.use(express.static("client/dist")); // parse post params sent in body in json format app.use(express.json());

/**

/**

/**

/**

async function handleResponse(response) { try { const jsonResponse = await response.json(); return { jsonResponse, httpStatusCode: response.status, }; } catch (err) { const errorMessage = await response.text(); throw new Error(errorMessage); } }

// serve index.html app.get("/", (req, res) => { res.sendFile(path.resolve("./client/dist/index.html")); });

// return client token for hosted-fields component app.post("/api/token", async (req, res) => { try { const { jsonResponse, httpStatusCode } = await generateClientToken(); res.status(httpStatusCode).json(jsonResponse); } catch (error) { console.error("Failed to generate client token:", error); res.status(500).send({ error: "Failed to generate client token." }); } });

app.post("/api/orders", async (req, res) => { try { // use the cart information passed from the front-end to calculate the order amount detals const { cart } = req.body; const { jsonResponse, httpStatusCode } = await createOrder(cart); res.status(httpStatusCode).json(jsonResponse); } catch (error) { console.error("Failed to create order:", error); res.status(500).json({ error: "Failed to create order." }); } });

app.post("/api/orders/:orderID/capture", async (req, res) => { try { const { orderID } = req.params; const { jsonResponse, httpStatusCode } = await captureOrder(orderID); res.status(httpStatusCode).json(jsonResponse); } catch (error) { console.error("Failed to create order:", error); res.status(500).json({ error: "Failed to capture order." }); } });

app.listen(PORT, () => { console.log(Node server listening at http://localhost:${PORT}/); });