WebFund 2016W Lecture 13: Difference between revisions

From Soma-notes
No edit summary
No edit summary
Line 74: Line 74:


===storeLogs.js===
===storeLogs.js===
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/storeLogs.js downloadable version]


<source lang="javascript" line>
<source lang="javascript" line>

Revision as of 21:36, 1 March 2016

Video

The video from the lecture given on March 1, 2016 is now available.

Notes

In lecture

Lecture 13
----------

To get help with assignment 4, look at Assignment 5 from Winter 2015.

closures

DOM
 - document object model
 - set of JavaScript objects for accessing the current page in the browser
 
client-side JS can access the DOM
  (data structures representing the current web page)

server-side JS can access the operating system

client-side JS is *limited* in its access
 - because such access is dangerous
 - ANYONE can run JavaScript in your browser
 - so, JavaScript in the browser is sandboxed


execution sandbox is an environment for running untrusted programs
 - sandbox limits access and resources

Client-side JavaScript has no native way to
  (well, it used to...)
  - access local files
  - open network connections
  - run multiple threads
  - access other windows/programs


Originally JavaScript was so limited it could do no background processing
 - could only run when the page was loaded or when the user acted

Microsoft messed it up
 - Outlook Web Access
 - they built an ActiveX control which implemented XMLHttpRequest()
   - but really, it was a way to do GETs and POSTs in the background
 - other browsers implemented the API natively

Now you could update data in a web page without reloading the page

Code

nested.js

var f = function(x) {
    function g(y) {
	return x + y;
    }
    
    return g;
}

var q = f(7);
var h = f(12);

console.log("q(4) = " + q(4));
console.log("h(4) = " + h(4));


storeLogs.js

downloadable version

// storeLogs.js
//
// node storeLogs.js <logfile>
//
//
// The log messages in the file should be of the format:
// <month> <day of month> <24-hour time in hh:mm:ss> <host> <service name[pid]>: Actual message
//

var mc = require('mongodb').MongoClient;
var fs = require('fs');

var data = fs.readFileSync(process.argv[2], 'utf-8');

var lines = data.split('\n');

var entries = [];

var i, j, entry, field;

for (i=0; i<lines.length; i++) {
    if (lines[i] && lines[i] !== '') {
	field = lines[i].split(' ');
	entry = {};
	j = 0;
	while (j < field.length) {
	    if (field[j] === "") {
		field.splice(j, 1);
	    } else {
		j++;
	    } 
	}
	entry.date = field[0] + " " + field[1];
	entry.time = field[2];
	entry.host = field[3];
	entry.service = field[4].slice(0,-1);
	entry.message = field.slice(5).join(' ');
	entries.push(entry);
    }
}

// entry.date = process.argv[2] + " " + process.argv[3];
// entry.time = process.argv[4];
// entry.host = process.argv[5];
// entry.service = process.argv[6].slice(0,-1);  // drop the trailing colon
// entry.message = process.argv.slice(7).join(' ');

var db;

var reportInserted = function(err, result) {
    if (err) {
	throw err;
    }

    console.log("Inserted the following log record:");
    console.log(result.ops);
    db.close();
}

var connectCallback = function(err, returnedDB) {
    if (err) {
	throw err;
    }

    db = returnedDB;
 
    db.collection('logs').insert(entries, reportInserted);
}

mc.connect('mongodb://localhost/log-demo', connectCallback);