progs/App2.js
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 15 Oct 2015 14:06:47 +0100
changeset 412 c30668124312
parent 172 aa7ddfee35c9
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
app.get('/', function(req, res){
172
aa7ddfee35c9 fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 163
diff changeset
     9
    var counter = parseInt(req.cookies.counter) || 0;
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
    res.cookie('counter', counter + 1);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    if (counter >= 5) {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
	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
    13
		  'visting the site ' + counter + ' times.');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    } else {
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
	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
    16
    }
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    res.end();
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
});
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
// starting the server
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
app.listen(8000);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
console.log("Server running at http://127.0.0.1:8000/");