-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (102 loc) · 2.59 KB
/
server.js
File metadata and controls
112 lines (102 loc) · 2.59 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const express = require('express');
const mustacheExpress = require('mustache-express');
const bodyParser = require('body-parser')
const app = express();
// tell our to use mustahce
app.engine('mst', mustacheExpress());
app.set('views', './views');
app.set('view engine', 'mst');
// define a public folder
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extend:false}))
// Creating a route for my website
app.get('/', (req, res) => {
res.render('index',allRobots[1]);
});
// This array is only for demostration purposes
// in the `future` this would a database
let allRobots = [
{
id:1,
name: 'R2-D2',
colors: ['blue', 'white'],
languages: 'beep-boop'
},{
id:2,
name: 'R4',
colors: ['gold', 'green', 'white']
},
{
id:3,
name: 'BB-8',
colors: ['orange', 'white']
},
{
id:4,
name: 'C3-PO',
colors: ['gold', 'silver', 'red'],
languages: 'yes'
}
];
// Creating an endpoint for my API
// this endpoint is where the back end
// meets the front end
// the front can see this url (/api/robot)
// the front can see the return data as JSON
app.get('/api/robots/:id', (req, res) => {
const robotId = parseInt(req.params.id)
// go my "database"
const myRobot = allRobots.find(bot => {
return bot.id === robotId
})
// this is how we retrun JSON from an endpoint
res.json(myRobot);
});
// RESTFUL - GET all robots
app.get('/api/robots', (req, res) => {
res.json(allRobots);
});
// // NOT RESTUL endpoints DONT DO THIS!!!!!!
// app.post('/api/get/robots/all', (req, res) => {
// const allRobots = [
// {
// name: 'R4',
// colors: ['gold', 'green', 'white']
// },
// {
// name: 'BB-8',
// colors: ['orange', 'white']
// }
// ];
// res.json(allRobots);
// })
// RESTful endpoints Are going here
// CREATE a robot
app.post('/api/robots', (req, res)=> {
// get data from the body
let newRobot = {
id: allRobots.length +1, // NOTE: the new ID will be handled by the database
name:req.body.name,
colors:req.body.colors,
languages:req.body.languages
}
// add to our "database"
allRobots.push(newRobot)
// return something
res.json(newRobot)
});
// GET a robots colors
// UPdate a robot
// disassemble a robot
app.delete('/api/robots/:id', (req, res) => {
// get the id from the request
const robotId = parseInt(req.params.id)
// delete the robot from the "database"
allRobots = allRobots.filter(bot => bot.id !== robotId)
// return something??
res.json(allRobots)
})
app.listen(3000, () => {
console.log('We are rocking on 3000');
});