-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (41 loc) · 1.36 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
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express to run server and routes
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const serverPort = 3000;
// Start up an instance of app
// See https://expressjs.com/en/4x/api.html for overview, API, etc.
const app = express();
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Cors for cross origin allowance
app.use(cors());
// Initialize the main project folder
app.use(express.static('website'));
// Setup Server and start a server
const appserver = app.listen(serverPort, () => {
console.log(`Weather Journal app listening on port ${serverPort}`);
})
// Handle GET '/all'
app.get('/all', (req, res) => {
console.log('GET ALL');
res.send(JSON.stringify(projectData)); // FIX: Need to make sure response is JSON
});
function addPostData(req,res) {
console.log('POST ADD:');
projectData.temperature = req.body.temperature;
projectData.date = req.body.date;
projectData.userResponse = req.body.userResponse;
res.send(projectData);
}
// Handle POST '/add'
app.post('/add', addPostData); // TODO: Convert to arrow?
// debug testing
app.get('/hello', (req, res) => {
console.log(req);
});
// END