var express = require( 'express' ) ;
var cookieParser = require( 'cookie-parser' ) ;
var app = express( ) ;
app.use ( cookieParser( ) ) ;
var products = {
1 : { title: '삼성 겔럭시 S7' } ,
2 : { title: 'Apple iPhone 7' }
} ;
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> 쇼핑몰</ h1>
< ul> ${ output} </ ul>
< p>< a href= "/cart" title= "카트에 추가하기" > 카트</ a></ p> `
) ;
} ) ;
app.get ( '/cart' , function ( req, res) {
var cart = req.cookies .cart
if ( ! cart) {
res.send ( '카트가 없습니다!' ) ;
} else {
var output = '' ;
for ( var id in cart) {
output += `< li> ${ products[ id] .title } ( ${ cart[ id] } ) </ li> `;
}
}
res.send ( `
< h1> 카트</ h1>
< ul> ${ output} </ ul>
< a href= "/products" > 목록으로</ a> `
) ;
} ) ;
app.get ( '/cart/:id' , function ( req, res) {
var id = req.params .id ;
if ( req.cookies .cart ) {
var cart = req.cookies .cart ;
} else {
var cart = { } ;
}
if ( ! cart[ id] ) {
cart = 0 ;
}
cart[ id] = parseInt( cart[ id] ) + 1 ;
res.cookie ( 'cart' , cart) ;
res.send ( cart) ;
} ) ;
app.get ( '/count' , function ( req, res) {
if ( req.cookies .count ) {
var count = parseInt( req.cookies .count ) ;
} else {
var count = 0 ;
}
count = count + 1 ;
res.cookie ( 'count' , count) ;
res.send ( '카운트 : ' + count) ;
} ) ;
app.listen ( 3306 , function ( ) {
console.log ( 'Conneted 3306 port!' ) ;
} ) ;
dmFyIGV4cHJlc3MgPSByZXF1aXJlKCdleHByZXNzJyk7CnZhciBjb29raWVQYXJzZXIgPSByZXF1aXJlKCdjb29raWUtcGFyc2VyJyk7CnZhciBhcHAgPSBleHByZXNzKCk7CmFwcC51c2UoY29va2llUGFyc2VyKCkpOwp2YXIgcHJvZHVjdHMgPSB7CgkxOnt0aXRsZTon7IK87ISxIOqylOufreyLnCB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