-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (50 loc) · 1.42 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
// Express to run server and routes
const express = require('express');
// Start up an instance of app
const app = express();
/* Dependencies */
const bodyParser = require('body-parser')
/* 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
const cors = require('cors');
app.use(cors());
//Initialize the main project folder
app.use(express.static('website'));
const port = 8000;
// Spin up the server
const server = app.listen(port, listening);
// const server = app.listen(port, () => { console.log(`running on localhost: ${port}`) })
// Callback to debug
function listening() {
console.log("server running");
console.log(`running on localhost: ${port}`)
};
// Create JS object
const appData = {}
// Respond with JS object when a GET request is made to the homepage
app.get('/all', function (req, res) {
res.send(appData)
})
/*
1. Routes & GET Requests
TODO-ROUTES! */
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
res.send('hello world');
});
// POST method route
app.post('/', function (req, res) {
res.send('POST received')
});
//Setup a basic POST route in the server side code
const data = [];
app.post('/addMovie', addMovie)
function addMovie(req, res) {
data.push(req.body)
console.log(data)
}