채지성

fourth

Showing 1 changed file with 39 additions and 9 deletions
...@@ -12,29 +12,59 @@ app.listen(8080, function () { ...@@ -12,29 +12,59 @@ app.listen(8080, function () {
12 }) 12 })
13 13
14 let items=[]; 14 let items=[];
15 +let count = 0;
15 16
16 app.get('/', function(req, res){ 17 app.get('/', function(req, res){
17 res.send(items); 18 res.send(items);
18 -}); 19 +}); // 조회하는 RESTful API
19 app.post('/post', function(req, res){ 20 app.post('/post', function(req, res){
20 - let item = req.body; 21 + let item = { id: count , text: req.body};
21 - console.log(item.id); 22 + count = count + 1;
22 - console.log(item.text);
23 23
24 - if(item != ""){ items.push(item); } 24 + console.log(item);
25 +
26 + if(item != ""){
27 + items.push(item);
25 console.log("items: ", items); 28 console.log("items: ", items);
29 + res.send(items);
30 + } else {
31 + res.status(400);
32 + }
33 +}); // 할 일을 생성하는 RESTfulAPI
34 +app.put('/put/:id', function(req, res){
35 + let id = req.params.id;
36 + console.log(id);
37 + let iter_ = 0;
26 38
39 + items.forEach((elem, index)=>{
40 + if(elem.id == id){
41 + items[index].text = req.body.text;
42 + iter_ = iter_ + 1;
43 + const bool_ = true;
44 + }
45 + });
46 + if(bool_ == true){
47 + res.status(400);
48 + } else {
27 res.send(items); 49 res.send(items);
50 + }
51 +}); // todo에 적힌 내용을 수정하는 RESTfulAPI
28 52
29 -});
30 app.delete('/item/:id', function(req, res){ 53 app.delete('/item/:id', function(req, res){
31 let id = req.params.id; 54 let id = req.params.id;
32 console.log(id); 55 console.log(id);
56 + let count = 0;
33 57
34 items.forEach((elem, index)=>{ 58 items.forEach((elem, index)=>{
35 - if(elem.id == id){ items.splice(index, 1); } 59 + if(elem.id == id){
60 + items.splice(index, 1);
61 + count = count + 1;
62 + }
36 }); 63 });
37 - console.log(items);
38 64
65 + if(count == 0){
66 + res.status(400);
67 + } else {
39 res.send(items); 68 res.send(items);
40 -});
...\ No newline at end of file ...\ No newline at end of file
69 + }
70 +}); // todo를 삭제하는 RESTfulAPI
...\ No newline at end of file ...\ No newline at end of file
......