181 lines
6.1 KiB
JavaScript
181 lines
6.1 KiB
JavaScript
// $Revision: 16 $ -- $Date: 2016-02-08 15:14:12 -0600 (Mon, 08 Feb 2016) $
|
|
|
|
var http = require('http');
|
|
var sys = require('util');
|
|
var fs = require('fs');
|
|
var moniFilename = 'abc';
|
|
var querystring = require('querystring');
|
|
var q = require('q');
|
|
|
|
var deferredAllDone;
|
|
|
|
http.createServer(function(req, res) {
|
|
//debugHeaders(req);
|
|
|
|
deferredAllDone = q.defer();
|
|
deferredAllDone.promise.done(function(pos) {
|
|
res.end();
|
|
console.log("Response END. [" + pos + "]");
|
|
});
|
|
|
|
if (req.headers.accept && req.headers.accept === 'text/event-stream') {
|
|
var arrMatches;
|
|
arrMatches = req.url.match(new RegExp(/\/events\/(.*)$/));
|
|
if (arrMatches) {
|
|
moniFilename = arrMatches[1];
|
|
console.log("Monitoring:" + moniFilename);
|
|
sendSSE(req, res);
|
|
} else {
|
|
res.writeHead(404);
|
|
deferredAllDone.resolve("404");
|
|
}
|
|
} else {
|
|
console.log("RCV-HTML");
|
|
res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' ,'Access-Control-Allow-Origin': '*' });
|
|
arrMatches = req.url.match(new RegExp(/\/(save|raw)\/(.*)$/));
|
|
console.log("Matches", arrMatches);
|
|
if ((arrMatches) && (arrMatches.length === 3)) {
|
|
var func = arrMatches[1];
|
|
moniFilename = arrMatches[2];
|
|
if ("save" === func) {
|
|
console.log("SAVE-Data");
|
|
//sys.puts(sys.inspect(req));
|
|
var fullBody = '';
|
|
|
|
req.on('data', function(chunk) {
|
|
// append the current chunk of data to the fullBody variable
|
|
fullBody += chunk.toString();
|
|
});
|
|
|
|
//req.on('end', (function(res) {
|
|
req.on('end', function() {
|
|
var decodedBody = querystring.parse(fullBody);
|
|
//sys.puts(sys.inspect(decodedBody));
|
|
|
|
fs.writeFile(__dirname + "/docs/" + moniFilename, decodedBody.data, function(err) {
|
|
if (err) {
|
|
res.write('ERROR : "' + err + '"');
|
|
deferredAllDone.resolve("OnEndERR");
|
|
console.log(err);
|
|
} else {
|
|
fs.stat(__dirname + "/docs/" + moniFilename, function(err, stats) {
|
|
if (stats) {
|
|
console.log('deb' + stats.mtime);
|
|
res.write(constructJSON("Saved", stats.mtime));
|
|
}
|
|
deferredAllDone.resolve("OnEndSaved");
|
|
});
|
|
}
|
|
});
|
|
});
|
|
//}(res)));
|
|
} else if ("raw" === func) {
|
|
console.log("RAW");
|
|
try {
|
|
res.write(fs.readFileSync(__dirname + '/docs/' + moniFilename));
|
|
} catch (e) {
|
|
res.writeHead(404, { 'Content-Type': 'text/html' });
|
|
console.log("RAW - FAILED");
|
|
}
|
|
deferredAllDone.resolve("OnRaw");
|
|
}
|
|
} else {
|
|
var reqFile=req.url.replace("/wc/","");
|
|
|
|
console.log("Orig Requested File " + reqFile );
|
|
if ((reqFile!=='EventSource.js') && (reqFile!=='autosize.min.js') ) {
|
|
reqFile='display.html'
|
|
}
|
|
console.log("Requested File " + reqFile );
|
|
var contentType="text/html";
|
|
if (reqFile.match(/\.css/gi)) {
|
|
contentType="text/css";
|
|
}
|
|
if (reqFile.match(/\.js/gi)) {
|
|
contentType="application/javascript";
|
|
}
|
|
|
|
|
|
var filePath = __dirname + '/' + reqFile;
|
|
console.log("Full path " + filePath + " [" + contentType + "]" );
|
|
|
|
try {
|
|
var stat = fs.statSync(filePath);
|
|
|
|
var readStream = fs.createReadStream(filePath);
|
|
res.writeHead(200, {"Content-Type":contentType});
|
|
|
|
readStream.pipe(res); // this will do a res.end() by its own.
|
|
|
|
} catch (ex) {
|
|
res.writeHead(404,{"Content-Type":"text/html"});
|
|
res.write("Not found: " + reqFile );
|
|
deferredAllDone.resolve("OnDisplay");
|
|
console.log ("Not found: " + reqFile, ex);
|
|
}
|
|
}
|
|
}
|
|
}).listen(8001,'127.0.0.1');
|
|
|
|
// Send the server side event - consider that this response never ends, so no res.end() is called whatsowever!
|
|
function sendSSE(req, res) {
|
|
var theClient=req.socket.remoteAddress + ":" + req.socket.remotePort;
|
|
res.writeHead(200, {
|
|
'Content-Type': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
'Connection': 'keep-alive'
|
|
});
|
|
|
|
var id = (new Date()).toLocaleTimeString();
|
|
|
|
fs.stat(__dirname + "/docs/" + moniFilename, function(err, stats) {
|
|
if (stats) {
|
|
console.log('deb' + stats.mtime);
|
|
res.write(constructSSE("Modified", stats.mtime));
|
|
}
|
|
});
|
|
|
|
// Watching directory for changes
|
|
fs.watch(__dirname + '/docs/', function(event, filename) {
|
|
console.log('event is: ' + event);
|
|
if (filename) {
|
|
console.log('filename provided: ' + filename);
|
|
console.log("Monitoring:" + moniFilename);
|
|
|
|
if (moniFilename === filename) {
|
|
fs.stat(__dirname + "/docs/" + filename, function(err, stats) {
|
|
console.log('deb 2 ' +theClient + " - " + stats.mtime);
|
|
res.write(constructSSE("Modified", stats.mtime));
|
|
});
|
|
}
|
|
} else {
|
|
console.log('filename not provided');
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// the SSE requires a "data" element
|
|
function constructSSE(type, timeStamp) {
|
|
var retVal = "data: " + constructJSON(type, timeStamp) + "\n\n";
|
|
console.log('MESSAGE-DEBUG-L: ' + type + " _ " + timeStamp);
|
|
return (retVal);
|
|
}
|
|
|
|
// just the JSON of the data
|
|
function constructJSON(type, timeStamp) {
|
|
var retVal = '{"type": "' + type + '",';
|
|
retVal += ('"timeStamp": "' + timeStamp + '"}');
|
|
return (retVal);
|
|
}
|
|
|
|
function debugHeaders(req) {
|
|
sys.puts('URL: ' + req.url);
|
|
for (var key in req.headers) {
|
|
sys.puts(key + ': ' + req.headers[key]);
|
|
}
|
|
sys.puts('\n\n');
|
|
}
|