progs/App4.js
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 23 Sep 2014 11:23:29 +0100
changeset 169 2866fae8c1cf
parent 163 02026ee18378
child 178 13c6bd6e3477
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
var express = require('express');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
var cookie  = require('cookie-parser')
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
var crypto = require('crypto');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
var app = express();
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
app.use(cookie());
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
var salt = 'secret key' 
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
function mk_hash(s) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    return crypto.createHash('sha1').update(s).digest('hex')
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
}
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
function mk_cookie(c) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    return c.toString() + '-' + mk_hash(c.toString() + salt)
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
}
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
function gt_cookie(s) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    var splits = s.split("-", 2);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    var counter = parseInt(splits[0])
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    var hash = splits[1]
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    if (mk_hash(counter.toString() + salt) == hash) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
	return counter
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
    } else {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
	return 0
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
    }
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
}
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
app.get('/', function(req, res){
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    var counter = gt_cookie(req.cookies.counter) || 0;
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    res.cookie('counter', mk_cookie(counter + 1));
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    if (counter >= 5) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
	res.write('You are a valued customer ' + 
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
		  'visting the site ' + counter + ' times.');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    } else {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
	res.write('This is visit number '+ counter +'!');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    }
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    res.end();
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
});
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
// starting the server
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
app.listen(8000);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
console.log("Server running at http://127.0.0.1:8000/");