-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (36 loc) · 1.3 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
const express = require('express');
const formData = require('express-form-data');
const client = require('./elasticsearch/connection');
const path = require('path');
//Import Routes Here
const data = require('./routes/api/data')
const totals = require('./routes/api/data2')
const app = express();
client.ping({
requestTimeout: 30000,
}, function(error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('Elasticsearch is connected');
}
});
// Init Middleware
app.use(express.json({ extended: false }))
app.use(express.urlencoded({ extended: true }));
app.use(formData.parse())
//Define Routes
app.use('/api/data', data);
app.use('/api/data', totals);
//Serve Static assets in production
//Configuration for Express to behave correctly in production environment
if (process.env.NODE_ENV === 'production') {
//First - Making sure express will serve production assets - main.js, main.css, etc
app.use(express.static('client/build'));
//Second -Express will serve up the index.html file if it doesn't recognize the route
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
});
};
const PORT = process.env.PORT || 5002;
app.listen(PORT, () => console.group(` Server Started On ${PORT}`));