equal
deleted
inserted
replaced
|
1 var express = require('express'); |
|
2 var cookie = require('cookie-parser') |
|
3 var crypto = require('crypto'); |
|
4 |
|
5 var app = express(); |
|
6 app.use(cookie()); |
|
7 |
|
8 function mk_hash(s) { |
|
9 return crypto.createHash('sha1').update(s).digest('hex') |
|
10 } |
|
11 |
|
12 function mk_cookie(c) { |
|
13 return c.toString() + "-" + mk_hash(c.toString()) |
|
14 } |
|
15 |
|
16 function gt_cookie(s) { |
|
17 var splits = s.split("-", 2); |
|
18 var counter = parseInt(splits[0]) |
|
19 if (mk_hash(counter.toString()) == splits[1]) { |
|
20 return counter |
|
21 } else { |
|
22 return 0 |
|
23 } |
|
24 } |
|
25 |
|
26 |
|
27 app.get('/', function(req, res){ |
|
28 var counter = gt_cookie(req.cookies.counter) || 0; |
|
29 res.cookie('counter', mk_cookie(counter + 1)); |
|
30 if (counter >= 5) { |
|
31 res.write('You are a valued customer ' + |
|
32 'visting the site ' + counter + ' times.'); |
|
33 } else { |
|
34 res.write('This is visit number '+ counter +'!'); |
|
35 } |
|
36 res.end(); |
|
37 }); |
|
38 |
|
39 // starting the server |
|
40 app.listen(8000); |
|
41 console.log("Server running at http://127.0.0.1:8000/"); |