
๐ฏ ์ฑ๋ API๋ฅผ ์ค๊ณํ๊ณ ,
app.route()๋ฅผ ์ฌ์ฉํด ์ฝ๋ ๊ตฌ์กฐ๋ฅผ ์ ๋ฆฌํฉ๋๋ค.
์ฑ๋ ์์ฑ : POST /channels
request : body (channelTitle)
response : 201 Created ${channelTitle}๋ ์ฑ๋์ ์์ํฉ๋๋ค. -> ์ฑ๋ ๊ด๋ฆฌ ํ์ด์ง
์ฑ๋ ์์ : PUT /channels/:id
request: URL (id), body(channelTitle)
response : 200 Ok ์ฑ๋๋ช
์ด ์ฑ๊ณต์ ์ผ๋ก ์์ ๋์์ต๋๋ค. ๊ธฐ์กด${} => ์์ ${}
์ฑ๋ ๊ฐ๋ณ ์ญ์ : DELETE /channels/:id
request: URL(id)
response : 200 Ok ์ญ์ ๋์์ต๋๋ค. => ๋ฉ์ธ ํ์ด์ง
์ฑ๋ ์ ์ฒด ์กฐํ : GET /channels
request: x
response : 200 Ok ์ฑ๋ ์ ์ฒด ๋ฐ์ดํฐ list, json array
์ฑ๋ ๊ฐ๋ณ ์กฐํ : GET /channels/:id
request: URL(id)
response : ์ฑ๋ ๊ฐ๋ณ ๋ฐ์ดํฐ
const express = require('express');
const app = express();
app.listen(7777);
app.use(express.json());
let db = new Map();
let id = 1;
Express๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ๋ฅผ ์์ฑํ๊ณ , Map ๊ฐ์ฒด๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
/channels)app
.route('/channels')
.post((req, res) => {
if (req.body.author) {
db.set(id++, req.body);
res.status(201).json({
message: `${db.get(id - 1).author}๋์ ๊ธ์ฐ๊ธฐ ์ํ์ ์์ํฉ๋๋ค.`,
});
} else {
res.status(400).json({
message: '์์ฒญ ๊ฐ์ ์ ๋๋ก ๋ณด๋ด์ฃผ์ธ์',
});
}
}) // ์๊ฐ ์ฑ๋ ๊ฐ๋ณ ์์ฑ
.get((req, res) => {
if (db.size) {
let channels = [];
db.forEach((value) => {
channels.push(value);
});
res.status(200).json(channels);
} else {
res.status(404).json({
message: '์๊ฐ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.',
});
}
}); // ์๊ฐ ์ฑ๋ ์ ์ฒด ์กฐํ
POST /channels : req.body.author๊ฐ ์กด์ฌํ๋ฉด ์๋ก์ด ์ฑ๋์ Map์ ์ ์ฅํฉ๋๋ค.
GET /channels : ์ ์ฅ๋ ๋ชจ๋ ์ฑ๋์ ๋ฐํํฉ๋๋ค. ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด 404 ์๋ต์ ๋ณด๋
๋๋ค.
โ
201 Created

โ 400 Bad Request

โ
200 OK

โ 404 Not Found

app
.route('/channels/:id')
.get((req, res) => {
const id = parseInt(req.params.id);
let channel = db.get(id);
if (channel) {
res.status(200).json(channel);
} else {
res.status(404).json({
message: '์๊ฐ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.',
});
}
}) // ์๊ฐ ์ฑ๋ ๊ฐ๋ณ ์กฐํ
.put((req, res) => {
const id = parseInt(req.params.id);
let channel = db.get(id);
if (!channel) {
res.status(404).json({
message: '์๊ฐ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.',
});
}
let existingAuthor = channel.author;
let newAuthor = req.body.author;
channel.author = newAuthor;
res.status(200).json({
message: `์๊ฐ ์ด๋ฆ์ด ์ ์์ ์ผ๋ก ์์ ๋์์ต๋๋ค ๊ธฐ์กด ${existingAuthor}์์ ${newAuthor}๋ก ์์ ๋์์ต๋๋ค.`,
});
}) // ์๊ฐ ์ฑ๋ ๊ฐ๋ณ ์์
.delete((req, res) => {
const id = parseInt(req.params.id);
let channel = db.get(id);
if (channel) {
db.delete(id);
res.status(200).json({
message: `${channel.author}(์ด)๊ฐ ์ญ์ ๋์์ต๋๋ค.`,
});
} else {
res.status(404).json({
message: '์๊ฐ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.',
});
}
}); // ์๊ฐ ์ฑ๋ ๊ฐ๋ณ ์ญ์
GET /channels/:id : ํน์ id์ ์ฑ๋์ ์กฐํํฉ๋๋ค.
PUT /channels/:id : ํน์ id์ ์ฑ๋์ ์์ ํฉ๋๋ค. ๊ธฐ์กด author๊ณผ ์๋ก์ด author๋ฅผ ์ถ๋ ฅํด ๋ณ๊ฒฝ๋ ๋ด์ฉ์ ์๋ ค์ค๋๋ค.
DELETE /channels/:id: ํน์ id์ ์ฑ๋์ ์ญ์ ํฉ๋๋ค.
โ
200 OK

โ 404 Not Found

โ
200 OK

โ 404 Not Found

โ
200 OK

โ 404 Not Found

1๏ธโฃ. Object.keys() (โ
์ถ์ฒ)
2๏ธโฃ. for ...in
3๏ธโฃ. lodash ๋ผ์ด๋ธ๋ฌ๋ฆฌ : _.isEmpty
const obj1 = {}
const obj2 = { message: 'hello' };
const str1 = 'one';
const str2 = '';
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
console.log(isEmpty(obj1)); // true
console.log(isEmpty(obj2)); // false
console.log(isEmpty(str1)); // false
console.log(isEmpty(str2)); // true
๋์ผํ URL์ ๊ธฐ์ค์ผ๋ก app.route()๋ฅผ ์ฌ์ฉํด API ์๋ํฌ์ธํธ๋ฅผ ๋ฌถ์ผ๋, ์ฝ๋๊ฐ ๋ ๊น๋ํ๊ณ ๊ตฌ์กฐ์ ์ผ๋ก ์ ๋ฆฌ๋์ด ์ ์ง๋ณด์๋ ๋ ์ฌ์ธ ๊ฒ ๊ฐ๋ค.
