nodejs 3

인프런 - shoppingCart - 4

cookie를 사용해서 cart에 cookie 값을 넣어보자! app.get('/cart', function(req, res){ // cart의 쿠키값 전달 var cart = req.signedCookies.cart if(!cart){ res.send('EMPTY!') } else { var output = '' for(var id in cart){ // id는 제품의 id값 output += `${products[id].title} (${cart[id]})` } res.send(`CART${output}Products List`) } }) cart에 cart cookie의 값을 전달해줍니다 만약 cart cookie에 값이 없다면 EMPTY를 브라우저에 전달합니다. 값이 존재하면 for ~ in문을 ..

Node.js 2023.12.19

인프런 - shoppingCart - 3

cookie를 사용해서 cart에 항목들을 넣어보자! app.get('/cart/:id', function(req, res){ var id = req.params.id if(req.signedCookies.cart){ var cart = req.signedCookies.cart } else { var cart = {} } if(!cart[id]) cart[id] = 0 cart[id] = parseInt(cart[id]) + 1 res.cookie('cart', cart, {signed:true}) res.redirect('/cart') }) products에서 품목을 누르면 품목의 cart/name 즉, 품목의 name으로 이동을 하기 때문에 /cart/:id로 접속을 해줍니다! id에 요청의 para..

Node.js 2023.12.19

인프런 - shoppingCart - 2

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 += `${products[name].title}` } res.send(`Products${output}CART`) }) 간단한 예제이기 때문에 DB 연결은 하지 않고 products 객체를 생성해서 사용 for ~ in문을 사용하여 products에 있는 값을 순회합니다! 이때 name -> 1 products[name] -> { title: ..

Node.js 2023.12.19