# HG changeset patch # User Christian Urban # Date 1411430760 -3600 # Node ID 02026ee1837808b3d7532f6c0516ed9d760b368b # Parent 5031e7778fdb400e24e7a8a4a572821cae5311a3 added js files diff -r 5031e7778fdb -r 02026ee18378 progs/App0.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/App0.js Tue Sep 23 01:06:00 2014 +0100 @@ -0,0 +1,14 @@ +var express = require('express'); +var app = express(); + +app.get('/', function(request, response){ + response.write('Hello World'); + response.end() +}); + +// alternative response +//response.write('

Hello World

'); + +// starting the server +app.listen(8000); +console.log("Server running at http://127.0.0.1:8000/"); \ No newline at end of file diff -r 5031e7778fdb -r 02026ee18378 progs/App1.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/App1.js Tue Sep 23 01:06:00 2014 +0100 @@ -0,0 +1,27 @@ +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 = '
' + + 'Login:
' + + 'Password:
' + + '' + + '
'; + res.send(html); +}); + +// receiving data +app.post('/', function(req, res){ + var html = 'Received login: ' + req.body.login + '
' + + 'Received password: ' + req.body.pass + '
' + + 'Try again'; + res.send(html); +}); + +// starting the server +app.listen(8000); +console.log("Server running at http://127.0.0.1:8000/"); \ No newline at end of file diff -r 5031e7778fdb -r 02026ee18378 progs/App2.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/App2.js Tue Sep 23 01:06:00 2014 +0100 @@ -0,0 +1,22 @@ +var express = require('express'); +var cookie = require('cookie-parser') +var crypto = require('crypto'); + +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(); +}); + +// starting the server +app.listen(8000); +console.log("Server running at http://127.0.0.1:8000/"); diff -r 5031e7778fdb -r 02026ee18378 progs/App3.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/App3.js Tue Sep 23 01:06:00 2014 +0100 @@ -0,0 +1,41 @@ +var express = require('express'); +var cookie = require('cookie-parser') +var crypto = require('crypto'); + +var app = express(); +app.use(cookie()); + +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)); + 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(); +}); + +// starting the server +app.listen(8000); +console.log("Server running at http://127.0.0.1:8000/"); diff -r 5031e7778fdb -r 02026ee18378 progs/App4.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/App4.js Tue Sep 23 01:06:00 2014 +0100 @@ -0,0 +1,44 @@ +var express = require('express'); +var cookie = require('cookie-parser') +var crypto = require('crypto'); + +var app = express(); +app.use(cookie()); + +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){ + var counter = gt_cookie(req.cookies.counter) || 0; + res.cookie('counter', mk_cookie(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(); +}); + +// starting the server +app.listen(8000); +console.log("Server running at http://127.0.0.1:8000/");