-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (37 loc) · 1.41 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');
// Start up an instance of app
const app = express();
/* Dependencies */
// Here we are configuring express to use body-parser as middle-ware.
// Body-parser allow the backend to access JSON data send from the client using request.body in POST route handler
const bodyParser = require('body-parser');
// Parse applicaiton/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// Parse application/json
app.use(bodyParser.json());
// CORS allow us to manage a cross-origin resource sharing policy so that the frontend(client) can talk to the server
const cors = require('cors');
// Enable all CORS requests
app.use(cors());
// Initialize the main project folder
app.use(express.static('website'));
// GET route to return the ProjectData endpoint
const getData = (request, response) => response.send(projectData);
app.get('/all', getData);
// POST route to add new data to the ProjectData endpoint
const addData = (request, response) => {
projectData = request.body;
console.log(projectData);
response.send(projectData);
}
app.post('/add', addData);
// Setup Server
const port = 8000;
const hostname = "127.0.0.1";
// Listenning to the server
app.listen(port, () => {
console.log(`Server running at: http://${hostname}:${port}/`)
});