forked from stripe-archive/stripe-payments-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (57 loc) · 1.85 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
/**
* server.js
* Stripe Payments Demo. Created by Romain Huet (@romainhuet)
* and Thorsten Schaeff (@thorwebdev).
*
* This is the main file starting the Express server for the demo and enabling ngrok.
*/
'use strict';
const config = require('./config');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const ngrok = config.ngrok.enabled ? require('ngrok') : null;
const app = express();
// Setup useful middleware.
app.use(
bodyParser.json({
// We need the raw body to verify webhook signatures.
// Let's compute it only when hitting the Stripe webhook endpoint.
verify: function(req, res, buf) {
if (req.originalUrl.startsWith('/webhook')) {
req.rawBody = buf.toString();
}
},
})
);
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, '../../public')));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// Define routes.
app.use('/', require('./routes'));
// Start the server on the correct port.
const server = app.listen(config.port, () => {
console.log(`🚀 Server listening on port ${server.address().port}`);
});
// Turn on the ngrok tunnel in development, which provides both the mandatory HTTPS
// support for all card payments, and the ability to consume webhooks locally.
if (ngrok) {
ngrok
.connect({
addr: config.ngrok.port,
subdomain: config.ngrok.subdomain,
authtoken: config.ngrok.authtoken,
})
.then(url => {
console.log(`💳 App URL to see the demo in your browser: ${url}/`);
})
.catch(err => {
if (err.code === 'ECONNREFUSED') {
console.log(`⚠️ Connection refused at ${err.address}:${err.port}`);
} else {
console.log(`⚠️ Ngrok error: ${JSON.stringify(err)}`);
}
process.exit(1);
});
}