// νμΌμ΄λ¦ : 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;