-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (34 loc) · 1.15 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
const { createServer } = require('http');
const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const path = require('path');
const normalizePort = port => parseInt(port, 10);
const PORT = normalizePort(process.env.PORT || 5000);
const app = express();
const dev = app.get('env') !== 'production';
if (! dev) {
app.disable('x-powered-by');
app.use(compression());
app.use(morgan('common'));
app.use(function forceLiveDomain(req, res, next) {
// Don't allow user to hit Heroku now that we have a domain
const host = req.get('Host');
if (host === 'bp-reactjs-portfolio.herokuapp.com') {
return res.redirect(301, 'https://bobbypatterson.me' + req.originalUrl);
}
return next();
});
app.use(express.static(path.resolve(__dirname, 'build'), {maxAge: '30d'}));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
if (dev) {
app.use(morgan('dev'));
}
const server = createServer(app);
server.listen(PORT, err => {
if (err) throw err;
console.log("Server started");
});