--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/ap0.js Tue Sep 23 02:04:34 2014 +0100
@@ -0,0 +1,10 @@
+var express = require('express');
+var app = express();
+
+app.get('/', function(request, response){
+ response.write('Hello World');
+ response.end()
+});
+
+// starting the server
+app.listen(8000);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/ap1.js Tue Sep 23 02:04:34 2014 +0100
@@ -0,0 +1,21 @@
+var express = require('express');
+var bodyParser = require('body-parser');
+var app = express();
+app.use(bodyParser.urlencoded({ extended: true }));
+
+// sending the form
+app.get('/', function(req, res){
+ var html = '<form action="/" method="post">' +
+ 'Login: <input type="text" name="login" /><br>' +
+ 'Password: <input type="password" name="pass" /><br>' +
+ '<button type="submit">Submit</button></form>';
+ res.send(html);
+});
+
+// receiving data
+app.post('/', function(req, res){
+ var html = 'Received login: ' + req.body.login + '<br>' +
+ 'Received password: ' + req.body.pass + '<br>' +
+ '<a href="/">Try again</a>';
+ res.send(html);
+});
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/ap2.js Tue Sep 23 02:04:34 2014 +0100
@@ -0,0 +1,19 @@
+var express = require('express');
+var cookie = require('cookie-parser')
+
+var app = express();
+app.use(cookie());
+
+app.get('/', function(req, res){
+ var counter = req.cookies.counter || 0;
+ res.cookie('counter', counter + 1);
+ if (counter >= 5) {
+ res.write('You are a valued customer ' +
+ 'visting the site ' + counter + ' times.');
+ } else {
+ res.write('This is visit number '+ counter +'!');
+ }
+ res.end();
+});
+
+app.listen(8000);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/ap3.js Tue Sep 23 02:04:34 2014 +0100
@@ -0,0 +1,22 @@
+function mk_hash(s) {
+ return crypto.createHash('sha1').update(s).digest('hex')
+}
+
+function mk_cookie(c) {
+ return c.toString() + "-" + mk_hash(c.toString())
+}
+
+function gt_cookie(s) {
+ var splits = s.split("-", 2);
+ var counter = parseInt(splits[0])
+ if (mk_hash(counter.toString()) == splits[1]) {
+ return counter
+ } else { return 0 }
+}
+
+app.get('/', function(req, res){
+ var counter = gt_cookie(req.cookies.counter) || 0;
+ res.cookie('counter', mk_cookie(counter + 1));
+ ...
+});
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/ap4.js Tue Sep 23 02:04:34 2014 +0100
@@ -0,0 +1,20 @@
+var salt = 'secret key'
+
+function mk_hash(s) {
+ return crypto.createHash('sha1').update(s).digest('hex')
+}
+
+function mk_cookie(c) {
+ return c.toString() + '-' + mk_hash(c.toString() + salt)
+}
+
+function gt_cookie(s) {
+ var splits = s.split("-", 2);
+ var counter = parseInt(splits[0])
+ var hash = splits[1]
+ if (mk_hash(counter.toString() + salt) == hash) {
+ return counter
+ } else { return 0 }
+}
+
+app.get('/', function(req, res){ .... });