progs/App4.js
author Christian Urban <urbanc@in.tum.de>
Tue, 26 Sep 2017 12:47:25 +0100
changeset 538 456d1d6676f9
parent 530 6e08ee0d399d
permissions -rw-r--r--
update
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) {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    23
        return counter
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
    } else {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    25
        return 0
163
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
app.get('/', function(req, res){
530
6e08ee0d399d fixed bug
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
    30
    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
    31
    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
    32
    if (counter >= 5) {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    33
        res.write('You are a valued customer ' +
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    34
                  'visting the site ' + counter + ' times.');
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    } else {
178
13c6bd6e3477 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
    36
        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
    37
    }
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    res.end();
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
});
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
// starting the server
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
app.listen(8000);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
console.log("Server running at http://127.0.0.1:8000/");