-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (36 loc) · 1.09 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
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express to run server and routes
const express = require("express");
// Start up an instance of app
const app = express();
/* Middleware*/
const bodyParser = require("body-parser");
//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
const cors = require("cors");
app.use(cors());
// Initialize the main project folder
app.use(express.static("website"));
// Setup Server
const port = 8080;
app.listen(port, function () {
console.log(`server run at: http://localhost:${port}/`);
});
// get route
app.get("/get-all-data", function (req, res) {
res.send(projectData);
});
// post route
app.post("/sava-data", function (req, res) {
projectData.temperature = req.body.temperature;
projectData.date = req.body.date;
projectData.userResponse = req.body.userResponse;
res.send(projectData);
});
//all data route
app.get("/allData", (req, res) => {
res.send(projectData);
});