-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (48 loc) · 1.39 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
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express, CORS and Body Parser
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
// Configure the server port and cross origin allowance
const PORT = 3000;
// Start up an instance of app
const app = express();
/* Middleware*/
// Configure body-parser as middleware
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(cors())
// Initialize the main project folder
app.use(express.static('website'));
// Setup Server
app.listen(PORT, () => {
console.log(`Server is listening on ${PORT}`);
});
app.get('/all', (req, res) => {
try {
res.status(200).send(projectData);
} catch (error) {
console.error(JSON.stringify(error));
res.status(500).send();
}
});
app.post('/', (req, res) => {
try {
const { temperature, date, userResponse } = req.body;
if (!(temperature && date && userResponse)) {
throw new Error('temperature, date and userResponse are required fields');
}
projectData = {
temperature: temperature,
date: date,
userResponse: userResponse
};
res.status(200).send();
} catch (error) {
console.error(JSON.stringify(error));
res.status(500).send(error);
}
});