progs/App3.js
changeset 178 13c6bd6e3477
parent 163 02026ee18378
child 530 6e08ee0d399d
equal deleted inserted replaced
177:46e581d66f3a 178:13c6bd6e3477
    15 
    15 
    16 function gt_cookie(s) {
    16 function gt_cookie(s) {
    17     var splits = s.split("-", 2);
    17     var splits = s.split("-", 2);
    18     var counter = parseInt(splits[0])
    18     var counter = parseInt(splits[0])
    19     if (mk_hash(counter.toString()) == splits[1]) {
    19     if (mk_hash(counter.toString()) == splits[1]) {
    20 	return counter
    20         return counter
    21     } else {
    21     } else {
    22 	return 0
    22         return 0
    23     }
    23     }
    24 }
    24 }
    25 
       
    26 
    25 
    27 app.get('/', function(req, res){
    26 app.get('/', function(req, res){
    28     var counter = gt_cookie(req.cookies.counter) || 0;
    27     var counter = gt_cookie(req.cookies.counter) || 0;
    29     res.cookie('counter', mk_cookie(counter + 1));
    28     res.cookie('counter', mk_cookie(counter + 1));
    30     if (counter >= 5) {
    29     if (counter >= 5) {
    31 	res.write('You are a valued customer ' + 
    30         res.write('You are a valued customer ' + 
    32 		  'visting the site ' + counter + ' times.');
    31                   'visting the site ' + counter + ' times.');
    33     } else {
    32     } else {
    34 	res.write('This is visit number '+ counter +'!');
    33         res.write('This is visit number '+ counter +'!');
    35     }
    34     }
    36     res.end();
    35     res.end();
    37 });
    36 });
    38 
    37 
    39 // starting the server
    38 // starting the server