-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (24 loc) · 803 Bytes
/
Copy pathserver.js
File metadata and controls
33 lines (24 loc) · 803 Bytes
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
const express = require('express');
const app = express();
const packageJson = require('./package.json')
const router = express.Router();
// Middleware
app.use(requireHTTPS);
app.use(express.static('./build'));
// Redirect app request to build/index.html
app.get('/*', (req, res) => {
res.sendFile('index.html', {root: 'build'});
});
// Start server
app.listen(process.env.PORT || 8080, () => console.log('Server started...'));
/**
* @author: Klement Omeri
* Special thanks to Klement for providing the function to redirect traffic from http to https
*/
function requireHTTPS(req, res, next) {
// The 'x-forwarded-proto' check is for Heroku
if (!req.secure && req.get('x-forwarded-proto') !== 'https') {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}