-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
99 lines (83 loc) · 3.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
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
/* eslint-disable global-require, import/no-extraneous-dependencies */
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const mockMiddleware = require('./mock_endpoints/mockMiddleware');
const auth = require('./mock_endpoints/auth');
require('dotenv').config();
const app = express();
if (process.env.NODE_ENV === 'development') {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.dev.js');
const compiler = webpack(webpackConfig);
// Socket
const server = require('http').Server(app);
const Socket = require('./mock_endpoints/mockSocket');
// Create a socket connection for this server.
const socket = Socket(server);
// Attach socket to app for use in express middleware.
app.socket = socket;
app.use(
webpackDevMiddleware(compiler, {
hot: true,
publicPath: webpackConfig.output.publicPath,
}),
);
app.use(webpackHotMiddleware(compiler));
app.use(express.static(path.join(__dirname, '/')));
app.use(bodyParser.json());
// Incident routes
app.get('/api/incidents', auth, mockMiddleware.fetchIncidents);
app.get('/api/incidents/:id', auth, mockMiddleware.fetchIncident);
app.put('/api/incidents/:id', auth, mockMiddleware.updateIncident);
app.get('/api/search/incidents', auth, mockMiddleware.handleSearch);
// Notes routes
app.post('/api/incidents/:id/notes', mockMiddleware.addNoteToIncident);
app.get('/api/incidents/:id/notes', mockMiddleware.fetchIncidentNotes);
app.put('/api/notes/:id', mockMiddleware.editIncidentNote);
app.delete('/api/notes/:id', mockMiddleware.archiveIncidentNote);
// Chat routes
app.post('/api/incidents/:id/chats', mockMiddleware.addChatToIncident);
app.get('/api/incidents/:id/chats', mockMiddleware.fetchIncidentChats);
app.put('/api/chats/:id', mockMiddleware.editIncidentChat);
app.delete('/api/chats/:id', mockMiddleware.archiveIncidentChat);
// Staff routes
app.get('/api/users', mockMiddleware.fetchStaff);
app.post('/api/users/invite', mockMiddleware.addUser);
app.get('/api/users/search', mockMiddleware.searchUser);
app.put('/api/users/:id', mockMiddleware.editUser);
app.delete('/api/users/:id', mockMiddleware.deleteUser);
// roles
app.get('/api/roles', mockMiddleware.fetchRoles);
// locations
app.get('/api/locations', mockMiddleware.fetchLocations);
// Login route
app.post('/api/users/login', mockMiddleware.login);
app.use('*', (req, res, next) => {
const filename = path.join(compiler.outputPath, 'index.html');
compiler.outputFileSystem.readFile(filename, (err, result) => {
if (err) {
return next(err);
}
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
app.use((err, req, res) => {
res.status(500).send('Something broke!');
});
} else if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') {
// Configuration for production environment
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
const server = app.listen(process.env.PORT || 8080, () => {
const host = server.address().address;
const port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});