-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (57 loc) · 1.91 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Dependencies and modules
const express = require("express");
const path = require("path");
const cors = require("cors");
//Tells node that we are creating an express server
const app = express();
//Calls mongoose an odm for MongoDB
const mongoose = require("mongoose");
//Sets an initial PORT that will listen for requests from the client side
const PORT = process.env.PORT || 3001;
const routes = require("./routes");
//Middlewares = function that will be executed in between request and responses
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors({ origin: true }));
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
//STRIPE
const stripe = require("stripe")(process.env.STRIPE_TEST);
// const calculateOrderAmount = (items) => {
// console.log(items);
// // Replace this constant with a calculation of the order's amount
// // Calculate the order total on the server to prevent
// // people from directly manipulating the amount on the client
// return 1400;
// };
app.post("/create-payment-intent", async (req, res) => {
const { price } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: price * 100,
currency: "usd",
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
//API routes
app.use(routes);
//Api routes needs to be defined before DB connection
//Send every other request to the React app
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
const { MONGO_DB_URL } = process.env;
//Api routes needs to be defined before this runs
mongoose
.connect(MONGO_DB_URL)
.then(() => {
app.listen(PORT, () => {
console.log(`Mongodb connected and Server is running at ${PORT}.`);
});
})
.catch((err) => {
console.error("MongoDB connection error.");
});