cookie를 사용해서 쇼핑 카트 구현하기!
var products = {
1: {title : 'Goods1'},
2: {title : 'Goods2'},
3: {title : 'Goods3'}
}
app.get('/products', function(req, res) {
var output = ''
for(var name in products){
output += `<li><a href="/cart/${name}">${products[name].title}</a></li>`
}
res.send(`<h1>Products</h1><ul>${output}</ul><a href="/cart">CART</a>`)
})
간단한 예제이기 때문에 DB 연결은 하지 않고 products 객체를 생성해서 사용
for ~ in문을 사용하여 products에 있는 값을 순회합니다!
이때
name -> 1
products[name] -> { title: 'Goods1' }
products[name].title -> Goods1
입니다! 제가 헷갈려서 적어봤어요 *^____^*
output에는 <li>~</li> 전체 문장들이 다 들어갑니다!
output을 브라우저에 전송해주면
이런 결과가 나옵니다!
` ${ 변수명 } ` 을 사용하면 문자열 사이에 쉽게 변수를 넣어줄 수 있습니다!
'Node.js' 카테고리의 다른 글
인프런 - Login - 2 (0) | 2023.12.21 |
---|---|
인프런 - Login - 1 (0) | 2023.12.21 |
인프런 - shoppingCart - 4 (0) | 2023.12.19 |
인프런 - shoppingCart - 3 (0) | 2023.12.19 |
인프런 - cookie는 뭘까 🧐 - 1 (0) | 2023.12.19 |