forked from Montana-Code-School/FullStackDogs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (53 loc) · 1.39 KB
/
index.js
File metadata and controls
66 lines (53 loc) · 1.39 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
var dogs = [{ //data reserve
name: "Fido",
breed: "Doberman"
},
{
name: "Toby",
breed: "Beagle"
},
{
name: "Max",
breed: "Bulldog"
}];
var express = require('express');//makes coding node way nicer
var cors = require('cors'); // Takes care of headers
var bodyparser = require('body-parser'); // parses objects into json format
var app = express(); //run express
app.use(cors());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded()); //utf 8 encoding
app.use(express.static(__dirname + '/public')); // go to public folder in nodeproj1
//app.get('/url', function(req, res, next){next();}), function(req, res, next){}
app.get('/dogs', function(req, res, next){ //coresponding endpoint for a $.ajax({method: Get, url: "/dogs"}) on the front end.
res.send(dogs);
});
app.post('/dogs', function (req, res, next) {
dogs.push(req.body);
res.send(dogs);
});
app.put('/dogs/:id', function(req, res,next){
for(var i = 0; i < dogs.length; i++){
if(req.params.id === dogs[i].name){
dogs[i] = req.body;
}
}
res.send(dogs);
});
app.delete('/dogs/:id', function (req, res, next) {
var found =false;
for(var i = 0; i < dogs.length; i++){
if(req.params.id === dogs[i].name){
dogs.splice(i, 1);
found = true;
}
}
if (found){
res.send(dogs);
}else{
res.send('Id not found. Object not found');
}
});
app.listen(8080, function(){
console.log('Listening on port 8080');
});