Skip to content

Commit

Permalink
Added a simple Connect based NodeJS server to test the CORSAppender
Browse files Browse the repository at this point in the history
Updated the CORSAppender to buffer messages before sending
  • Loading branch information
bfattori committed Apr 16, 2013
1 parent 29c3fa1 commit ab01fe8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
33 changes: 26 additions & 7 deletions appenders/cors.js
Expand Up @@ -2,11 +2,14 @@

var CORSAppender = function(config) {
LogJS.BaseAppender.call(this);
this.xhr = new global.XMLHttpRequest();

this.xhr = config.__xhr;
this.config = {
targetUrl: this.configOpt('targetUrl', config, '')
targetUrl: this.configOpt('targetUrl', config, ''),
timeoutInterval: this.configOpt('timeoutInterval', config, 3000)
};

this.buffer = [];
this.timeout = undefined;
};

CORSAppender.prototype = Object.create(LogJS.BaseAppender.prototype);
Expand All @@ -15,18 +18,34 @@

CORSAppender.prototype.log = function(type, timestamp, message, url, lineNumber) {
var logObject = { 't': type, 'ts': timestamp, 'm': message, 'u': url, 'l': lineNumber};
var jsonString = JSON.stringify(logObject);
this.buffer.push(logObject);

global.clearTimeout(this.timeout);
var CAppender = this;
this.timeout = global.setTimeout(function() {
CAppender.sendBuffer();
CAppender.timeout = undefined;
}, this.timeoutInterval);
};

CORSAppender.prototype.sendBuffer = function() {
var jsonString = JSON.stringify(this.buffer);

if (this.config.targetUrl !== '') {
this.xhr.open('POST', this.config.targetUrl, true);
this.xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
this.xhr.setRequestHeader("Content-length", jsonString.length);
this.xhr.send(jsonString);
}

this.buffer = [];
};

if (global.XMLHttpRequest && ('withCredentials' in global.XMLHttpRequest)) {
LogJS.addAppender(CORSAppender);
if (global.XMLHttpRequest) {
var xhr = new global.XMLHttpRequest();
if ("withCredentials" in xhr) {
LogJS.config.__xhr = xhr;
LogJS.addAppender(CORSAppender);
}
}

})(LogJS, this);
6 changes: 6 additions & 0 deletions index.html
Expand Up @@ -6,10 +6,16 @@
<script type='text/javascript'>
LogJS.config.DOMAppender = {
fontSize: '9pt'
};

LogJS.config.CORSAppender = {
targetUrl: 'http://localhost:3000/logger'
}
</script>

<script type='text/javascript' src='appenders/dom.js'></script>
<script type='text/javascript' src='appenders/localstorage.js'></script>
<script type='text/javascript' src='appenders/cors.js'></script>
<script type='text/javascript' src='appenders/console.js'></script>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions server/.gitignore
@@ -0,0 +1 @@
/node_modules
17 changes: 17 additions & 0 deletions server/README.md
@@ -0,0 +1,17 @@
LogJS Testing Server for NodeJS
=====

## What is this?

A simple server for testing the CORS operation of the CORSAppender for LogJS.

## How do I use this?

In the `/server` folder, run the following:

npm install connect
npm install connect-xcors

Then you can run this server. It is configured to listen for any connections from any
origins at the moment. Not much of a concern because it's really just meant to be
a simple test.
32 changes: 32 additions & 0 deletions server/logserver.js
@@ -0,0 +1,32 @@
var connect = require('connect'),
http = require('http'),
CORS = require('connect-xcors'),
options = {
methods: ['POST']
}, logRef = 1;

var server = connect
.createServer(
connect.json(),
CORS(options),
function(req, res) {
var buf = req.body;
for (var idx = 0; idx < buf.length; idx++) {
var msg = buf[idx],
logMsg = '#' + logRef + ' [' + msg.t + '] ' + new Date(msg.ts).toString() + ': ' + msg.m;

if (msg.u) {
logMsg += ' (' + msg.u + ')';
}

if (msg.l) {
logMsg += ' @' + msg.l;
}

console.log(logMsg);
logRef++;
}
res.writeHead(200, { 'Content-Type': 'text/plain'});
res.end('LogJS Server');
}).listen(3000);

0 comments on commit ab01fe8

Please sign in to comment.