-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (45 loc) · 1.42 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
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
const enforce = require('express-sslify');
const isProduction = process.env.NODE_ENV === 'production';
const app = express();
const port = process.env.PORT || process.env.SERVER_PORT;
const distDir = path.join(__dirname, 'dist');
// middlewares
app.use(bodyParser.json());
app.use(compression());
if (isProduction) {
// Use enforce.HTTPS({ trustProtoHeader: true }) in case you are behind
// a load balancer (e.g. Heroku). See further comments below
app.use(enforce.HTTPS({
trustProtoHeader: true,
}));
}
// It will load bundle.js from the html but will receive bundle.js.gz
app.get('*.js', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
next();
});
// configure header
app.use((req, res, next) => {
const hostOrigin = req.headers.origin;
res.header('Access-Control-Allow-Origin', hostOrigin);
next();
});
// use static file dist
app.use(express.static(distDir));
// get static index.html
let distHTML = fs.readFileSync(path.join(distDir, 'index.html'), 'utf8');
// all other routes return our app html
app.get('*', (req, res) => {
res.send(distHTML);
});
app.listen(port, (err) => {
if(err) throw err;
console.log(`\nServer started on port ${port}.\n`);
});