author | cu |
Tue, 03 Oct 2017 14:50:45 +0100 | |
changeset 543 | 9439027a0736 |
parent 531 | 35ffb7a7eafa |
permissions | -rw-r--r-- |
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 | 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); |