progs/ap2.js
author Christian Urban <urbanc@in.tum.de>
Tue, 26 Sep 2017 10:36:19 +0100
changeset 531 35ffb7a7eafa
parent 174 e2180cead443
permissions -rw-r--r--
fixed bug
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
166
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
var express = require('express');
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
var cookie  = require('cookie-parser')
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
var app = express();
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
app.use(cookie());
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
app.get('/', function(req, res){
531
35ffb7a7eafa fixed bug
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
     8
    var counter =  parseInt(req.cookies.counter || "") || 0;
166
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
    res.cookie('counter', counter + 1);
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
    if (counter >= 5) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
        res.write('You are a valued customer ' + 
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
                  'visting the site ' + counter + ' times.');
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    } else {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
        res.write('This is visit number '+ counter +'!');
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    }
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
    res.end();
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
});
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
app.listen(8000);