-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (44 loc) · 1.33 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
const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const path = require('path');
const webpackConfig = require('./webpack.config.js');
const DB_URL = require('./database/url.js');
const db = require('./database/db.js');
const app = express();
// initiates webapck using your config rules
const compiler = webpack(webpackConfig);
app.use(express.static(path.join(__dirname, '/public')));
// hot reloads your react app on files change
app.use(webpackDevMiddleware(compiler, {
hot: true,
filename: 'bundle.js',
publicPath: '/',
stats: {
colors: true,
},
historyApiFallback: true,
}));
app.get('/api', (request, response) => {
const data = db.get();
let result = {};
const guestHouse = data.collection('GuestHouse');
guestHouse.find({ }).toArray((error, documents) => {
result = documents[0].GuestHouses;
response.send(result);
});
});
app.get('/*', (request, response) => {
response.sendFile(path.resolve(__dirname, 'public', 'index.html'));
});
// connects with the database before starting the server
db.connect(DB_URL, (err) => {
if (err) {
console.log('Unable to connect to mongo');
process.exit(1);
} else {
app.listen(process.env.PORT || 3000, () => {
console.log('Listening on port 3000');
});
}
});