This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (88 loc) Β· 2.9 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require('dotenv').config();
if (!process.env.SERVER_IP) {
console.error('β Failed to load in SERVER_IP. Is it missing from the `.env` file?');
process.exit();
}
const { SERVER_IP, NODE_ENV, PORT } = process.env;
const IN_PRODUCTION = NODE_ENV === 'production';
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const { events } = require('./server/songkick');
const { searchDice } = require('./server/dice');
const app = express();
app.use(bodyParser.json());
if (!IN_PRODUCTION) {
const webpack = require('webpack');
const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config');
const compiler = webpack(config);
app.use(devMiddleware(compiler, {
publicPath: config.output.publicPath,
historyApiFallback: true
}));
app.use(hotMiddleware(compiler));
}
if (IN_PRODUCTION) {
// redirect HTTP to HTTPS
app.enable('trust proxy');
app.use((req, res, next) => {
const usingHTTPS = req.secure;
if (!usingHTTPS) {
return res.redirect('https://songkick.pink');
}
return next();
});
}
// Static files
const staticFileDirectory = IN_PRODUCTION ? 'build' : 'src';
app.use(express.static(path.join(__dirname, staticFileDirectory)));
// allow CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
if (IN_PRODUCTION) {
// only allow from this ip address
app.use((req, res, next) => {
if (req.ip !== SERVER_IP) { // Wrong IP address
res.status(401);
return res.send('Permission denied. Wrong IP Address.');
}
next(); // correct IP address, continue middleware chain
});
}
app.get('/api', (req, res, next) => {
res.status(404).json({ error: 'Incorrect path. Did you mean /api/events or /api/dice?' });
});
app.get('/api/dice', (req, res) => {
const { searchTerm } = req.query;
if (!searchTerm) {
return res.status(404).json({ error: 'No search term sent' });
}
return searchDice(searchTerm)
.then(results => res.json(results))
.catch(error => res.status(500).json({ error }));
});
app.get('/api/events', (req, res, next) => {
const { username } = req.query;
if (!username) {
return res.status(404).json({ error: 'No username sent' });
}
return events(username)
.then(events => res.json(events))
.catch(error => res.status(500).json({ error }));
});
// Send everything else to react-router
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'build/index.html'));
});
app.listen(PORT || 8000, (err) => {
if (err) {
return console.error(err);
}
console.info(`π Listening at http://localhost:${PORT || 8000}/`);
console.info(`${IN_PRODUCTION ? 'π Production' : 'π Development'} mode`);
});