-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (45 loc) · 1.4 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
import express from 'express';
import path from 'path';
import stripe from 'stripe';
import admin from 'firebase-admin';
import { ApolloServer } from 'apollo-server';
import './env.js'
import { typeDefs } from './schema.js';
import { resolvers } from './resolver.js';
const __dirname = path.resolve();
const port = process.env.PORT || 5000;
const stripeFunc = stripe(process.env.STRIPE_SECRET_KEY);
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (_, response) => {
response.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
app.post('/payment', (request, response) => {
const body = {
source: request.body.token.id,
amount: request.body.amount,
currency: 'usd',
};
stripeFunc.charges.create(body, (stripeErr, stripeRes) => {
if (stripeErr) {
response.status(500).send({ error: stripeErr });
} else {
response.status(200).send({ error: stripeRes });
}
});
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
app.listen(port, error => {
if (error) throw error;
console.log('server running on port: ' + port);
});