-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (48 loc) · 1.51 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
const express = require("express"),
bodyParser = require("body-parser"),
cors = require("cors"),
path = require("path"),
compression = require("compression"),
enforce = require("express-sslify");
if (process.env.NODE !== "production") require("dotenv").config();
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const app = express();
const port = process.env.PORT || 5000;
//*****Middlewares*****
// app.use(compression);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(enforce.HTTPS({ trustProtoHeader: true }));
//*****Routes*****
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
app.get("/test", (req, res) => {
res.send({ test: "Server is working fine" });
});
app.get("/service-worker.js", (req, res) => {
res.sendFile(path.resolve(__dirname, "..", "build", "service-worker.js"));
});
app.post("/payment", (req, res) => {
// console.log("route hit");
const body = {
source: req.body.token.id,
amount: req.body.amount,
currency: "usd",
};
stripe.charges.create(body, (stripeErr, stripeRes) => {
if (stripeErr) {
res.status(500).send({ error: stripeErr });
} else {
res.status(200).send({ success: stripeRes });
}
});
});
app.listen(port, (err) => {
if (err) throw err;
console.log("Server Started at port : ", port);
});