-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (54 loc) · 1.52 KB
/
server.js
File metadata and controls
64 lines (54 loc) · 1.52 KB
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
'use strict';
const express = require('express');
const fetch = require('node-fetch');
const redirectToHTTPS = require('express-http-to-https').redirectToHTTPS;
// Change this to add a delay (ms) before the server responds.
const DISPATCH_DELAY = 0;
const BASE_URL = process.env.API_URL + '/api/dispatches';
/**
* Gets the dispatch data.
*
* @param {Request} req request object from Express.
* @param {Response} resp response object from Express.
*/
function getDispatches(req, resp) {
fetch(BASE_URL).then((resp) => {
if (resp.status !== 200) {
throw new Error(resp.statusText);
}
return resp.json();
}).then((data) => {
setTimeout(() => {
resp.json(data);
}, DISPATCH_DELAY);
}).catch((err) => {
console.error('Dispatch Error:', err.message);
});
}
/**
* Starts the Express server.
*
* @return {ExpressServer} instance of the Express server.
*/
function startServer() {
const app = express();
const port = process.env.PORT;
// Redirect HTTP to HTTPS,
app.use(redirectToHTTPS([/localhost:(\d{4})/], [], 301));
// Logging for each request
app.use((req, resp, next) => {
// eslint-disable-next-line no-console
console.log(resp);
next();
});
// Handle requests for the data
//app.get('/', getDispatches);
// Handle requests for static files
app.use(express.static('public'));
// Start the server
return app.listen(port, () => {
// eslint-disable-next-line no-console
console.log('Server started on port ' + port + '...');
});
}
startServer();