progs/App3.js
author cu
Mon, 09 Oct 2017 10:07:35 +0100
changeset 547 eae9a2e6ff28
parent 530 6e08ee0d399d
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
function mk_hash(s) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
    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
    10
}
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
function mk_cookie(c) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    return c.toString() + "-" + mk_hash(c.toString())
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
}
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
function gt_cookie(s) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    var splits = s.split("-", 2);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
    var counter = parseInt(splits[0])
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    if (mk_hash(counter.toString()) == splits[1]) {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    20
        return counter
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    } else {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    22
        return 0
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    }
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
app.get('/', function(req, res){
530
6e08ee0d399d fixed bug
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
    27
    var counter = gt_cookie(req.cookies.counter || "") || 0;
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    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
    29
    if (counter >= 5) {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    30
        res.write('You are a valued customer ' + 
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    31
                  'visting the site ' + counter + ' times.');
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    } else {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    33
        res.write('This is visit number '+ counter +'!');
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    }
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    res.end();
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
});
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
// starting the server
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
app.listen(8000);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
console.log("Server running at http://127.0.0.1:8000/");