-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (44 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
const express=require('express');
const app=express();
const cors=require('cors');
const bodyParser = require('body-parser');
const path= require('path');
const compression = require('compression'); // compresses all our static files which are then unzipped by client browser
const enforce=require("express-sslify");
if(process.env.NODE_ENV !== 'production')
require('dotenv').config();
const stripe=require('stripe')(process.env.STRIPE_SECRET_KEY);
const PORT=process.env.PORT || 3001;
app.use(compression()); // compresses all our static files which are then unzipped by client browser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cors());
if (process.env.NODE_ENV == "production") {
// basically it tells express app to serve all static files from curr_dir/client/build (Our react app in production is present in build folder after it is built an then this folder is deployed )
app.use(enforce.HTTPS({ trustProtoHeader: true })); // if anyone makes http request automatically redirected to HTTPS
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build", "index.html")); // if we get a GET request at any API(*) then send index.html file which contains our React Code(root Component)
});
}
app.get("/service-worker.js", (req, res) => {
res.sendFile(path.resolve(__dirname, "..", "build", "service-worker.js"));
});
app.post("/payment",(req,res)=>{
const body={
source: req.body.token.id,
amount: req.body.amount,
currency: 'inr'
};
stripe.charges.create(body,(err,stripeRes)=>{
if(err)
res.status(500).send({error:err});
else
res.status(200).send({success:stripeRes});
})
})
app.listen(PORT,error=>{
if(error)
throw error;
console.log(`Server is starting on port ${PORT}`);
})