server.js
1.58 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
// server.js
const express = require('express');
const app = express();
const cors = require(cors);
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.json());
app.use(cors({ origin:true }));
app.listen(8080, function () {
console.log('listening on 8080')
})
let items=[];
let count = 0;
app.get('/', function(req, res){
res.send(items);
}); // 조회하는 RESTful API
app.post('/post', function(req, res){
let item = { id: count , text: req.body};
count = count + 1;
console.log(item);
if(item != ""){
items.push(item);
console.log("items: ", items);
res.send(items);
} else {
res.status(400);
}
}); // 할 일을 생성하는 RESTfulAPI
app.put('/put/:id', function(req, res){
let id = req.params.id;
console.log(id);
let iter_ = 0;
items.forEach((elem, index)=>{
if(elem.id == id){
items[index].text = req.body.text;
iter_ = iter_ + 1;
const bool_ = true;
}
});
if(bool_ == true){
res.status(400);
} else {
res.send(items);
}
}); // todo에 적힌 내용을 수정하는 RESTfulAPI
app.delete('/item/:id', function(req, res){
let id = req.params.id;
console.log(id);
let count = 0;
items.forEach((elem, index)=>{
if(elem.id == id){
items.splice(index, 1);
count = count + 1;
}
});
if(count == 0){
res.status(400);
} else {
res.send(items);
}
}); // todo를 삭제하는 RESTfulAPI