-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (51 loc) · 1.49 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
//Packages
const bodyParser = require("body-parser");
const express = require("express");
const cors = require("cors");
//Instantiating the app
const app = express();
/* Middleware*/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
// Initialize the main project folder
app.use(express.static("src"));
// Setup Server
const port = 3000;
app.listen(port, listening);
function listening() {
console.log(`Your app is running on: http://localhost:${port}...`);
}
// Project data
const projectData = { history: [] };
//Get project data
app.get("/weather-history", getWeatherHistory);
function getWeatherHistory(req, res) {
return res.send(projectData.history);
}
//Get entry
app.get("/weather-reading/:id", getWeatherReading);
function getWeatherReading(req, res) {
const id = +req.params.id;
const reading = projectData.history.find((reading) => reading.id == id);
if (!reading) {
return res.status(404).send(`No reading with ${id} were found!`);
}
return res.send(reading);
}
//Post route to add new weather entry to the project data
app.post("/add-weather", postWeatherData);
function postWeatherData(req, res) {
const data = req.body;
const newEntry = {
id: projectData.history.length + 1,
zipCode: data.zipCode,
countryCode: data.countryCode,
feelings: data.feelings,
city: data.city,
date: data.date,
weather: data.weather,
};
projectData.history.push(newEntry);
return res.send(JSON.stringify(newEntry));
}