-
[데브코스] 23일차 TILTIL/교육 내용 정리 2024. 4. 30. 18:48
map object
String만 담던 Map
Key(id) Value(productName)
1 NoteBook + 2,000,000 2 Cup 3,000 3 Chair 100,000 4 Poster 20,000 - map에 두 개이상의 정보를 보낸다. 즉, 객체로 보낸다.
// map-demo.js let notebook ={ productName : "Notebook", price : 2000000 } let cup ={ productName : "cup", price : 3000 } let chair ={ productName : "Chair ", price : 100000 } let poster ={ productName : "Poster ", price : 20000 }
express +객체
//map-demo.js if (db.get(id) == undefined){ res.json({ message : "없는 상품입니다." }) }else{ product = db.get(id) product[""]//product.id = id -> id 까지 보여줌 res.json(product) }
- “key” 는 다 문자열로 처리되고 있음.
- 두 가지 방법으로 id 값을 보여줄 수 있음
- 순서는 상관 없음. 앞에 오나 뒤에 오나
express + map + 객체 추가 실습 : 유튜버
// youtuber-demo.js // express 모듈 셋팅 const express = require('express') const app = express() app.listen(3000) // 데이터 셋팅 let youtuber1 = { channelTitle : "십오야", sub : "593만명", videoNum : "993개" } let youtuber2 = { channelTitle : "침착맨", sub : "227만명", videoNum : "6.6천개" } let youtuber3 = { channelTitle : "테오", sub : "54.8만명", videoNum : "7260개" } let db = new Map() db.set(1, youtuber1) db.set(2,youtuber2) db.set(3, youtuber3) // REST API 설계 app.get('/youtuber/:id', function (req, res) { let {id} = req.params id = parseInt(id) const youtuber = db.get(id) if (youtuber == undefined){ res.json({ message : "유튜버 정보를 찾을 수 없습니다." }) }else{ res.json(youtuber) } })
이쯤에서 express 구조 이해해보기
- express는 http모듈에 이것저것 더해서 만든 모듈이다.
- express : 는 framework다
- 웹 프레임워크 : 내가 만들고 싶은 웹 서비스를 구현하는 데 필요한 모든 일을 틀 안에서 할 수 있는 것.
www, app.js 훑어보기
// app.js
generator 돌려보기
자바스크립트 함수 4가지 종류
// function-demo.js // ver1 function add1(x,y){ return x + y } // ver2 let add2 = function(x, y){ return x + y } // ver3 (화살표 함수, arrow function) const add3 = (x, y) =>{ return x + y } // ver4 var add4 = (x, y) => x + y console.log(add1(1,2)) console.log(add2(1,2)) console.log(add3(1,2)) console.log(add4(1,2))
// 출력 3 3 3 3
'TIL > 교육 내용 정리' 카테고리의 다른 글
[데브코스] 25일차 TIL (0) 2024.05.04 [데브코스] 24일차 TIL (0) 2024.05.04 [데브코스] 22일차 TIL (1) 2024.04.29 [데브코스] (20일차자체휴식)21일차 TIL (0) 2024.04.28 [데브코스] 16~18(예비군) 19일차 TIL (0) 2024.04.26