πŸ“

011_blog_API\router

// νŒŒμΌμ΄λ¦„ : 011_blog_API\router\blog.js const nunjucks = require('nunjucks'); const express = require('express'); let blogs = [{ id : 1, title : 'title1', content : 'content1', section : 'section1', pubDate : new Date().toString(), modDate : new Date().toString() }, { id : 2, title : 'title2', content : 'content2', section : 'section2', pubDate : new Date().toString(), modDate : new Date().toString() }, { id : 3, title : 'title3', content : 'content3', section : 'section3', pubDate : new Date().toString(), modDate : new Date().toString() }]; const router = express.Router(); // * / - 메인화면 졜근 κ²Œμ‹œλ¬Ό 3개 // * /blog - λΈ”λ‘œκ·Έ κΈ€ 리슀트 μ „λΆ€ router.get('/', (req, res, next) => { console.log(req.query); const section = req.query.section; const data = section ? blogs.filter(b => b.section === section) : blogs; res.status(200).json(data); }) // * /blog/?section=:section - ν•΄λ‹Ή μ„Ήμ…˜λ§Œ λͺ¨μ•„ λ³Ό 수 μžˆλ„λ‘ // * /about - λ‚΄ μ†Œκ°œ // * /blog/:id - λΈ”λ‘œκ·Έ 상세 κΈ€ // ex) http://localhost:8080/blog/1 router.get('/:id', (req, res, next) => { // console.log(req.params.id); const id = req.params.id; // console.log(blogs.find(b => b.id == 1)); const blog = blogs.find(b => b.id == id); //νƒ€μž…μ΄ number, string if (blog) { res.status(200).json(blog); } else { res.status(404).json({warninig : `${id}둜 μ ‘κ·Όν•˜λŠ” 것은 비정상적인 μ ‘κ·Όμž…λ‹ˆλ‹€.`}) } }) // * /write - λ‘œκ·ΈμΈμ‹œμ—λ§Œ ν™œμ„±ν™”(생성, μˆ˜μ •) -> post값은 /blog둜 router.post('/', (req, res, next) => { const id = blogs.length + 1; const title = req.body.title; // const {key1, key2...} = req.body; const content = req.body.content; const section = req.body.section; const pubDate = new Date().toString(); const modDate = new Date().toString(); // λ‘˜ λ‹€ 같을 λ•Œμ—λŠ” ν•˜λ‚˜ μƒλž΅ let blog = {id, title, content, section, pubDate, modDate}; blogs.push(blog); res.status(201).json(blog); // 201은 create /* postman test data post : localhost:8080/blog post, body, raw, json, data(μ•„λž˜) 확인 ν›„ send { "title" : "title4", "content" : "content4", "section" : "section4" } send ν›„ μ•„λž˜ url둜 μ ‘μ†ν•˜λ©΄ 4번이 λ“€μ–΄μ™€μžˆλŠ” 것 확인 localhost:8080/blog */ }) // μˆ˜μ •(PUT) : blog/:id router.put('/:id', (req, res, next) => { const id = req.params.id; const blog = blogs.find(b => b.id == id) if (blog){ // 각각의 값이 λΉ„μ–΄μžˆμ„ μˆ˜λ„ 있음 blog.title = req.body.title; blog.content = req.body.content; blog.section = req.body.section; blog.modDate = new Date().toString(); res.status(200).json(blogs); } else { res.status(404); } /* postman test data post : localhost:8080/blog/1 put, body, raw, json, data(μ•„λž˜) 확인 ν›„ send { "id" : 1, // ꡳ이 idκ°€ 듀어가지 μ•Šμ•„λ„ 됨. "title" : "title1-μˆ˜μ •", "content" : "content1-μˆ˜μ •", "section" : "section1-μˆ˜μ •" } μ‘λ‹΅κ°’μœΌλ‘œ λ°”λ‘œ 확인 κ°€λŠ₯ */ }) // μ‚­μ œ(DELETE) : blog/:id router.delete('/:id', (req, res, next) => { const id = req.params.id; blogs = blogs.filter(b => b.id != id) //μ•„λ‹Œ κ²ƒλ“€λ§Œ λ¬Άμ–΄μ£Όκ±°λ‚˜ remove, spliceλ₯Ό μ‚¬μš©ν•΄μ„œ μ‚­μ œ, filterκ°€ κ°€μž₯ 깔끔 res.status(200).json(blogs); //ꡳ이 204λ₯Ό 보내쀄거면 200둜 // postman test data // post : localhost:8080/blog/1 // delete, body, raw, json, data(ν•„μš”μ—†μŒ) 확인 ν›„ send }) module.exports = router;