Skip to content
This repository has been archived by the owner on Nov 27, 2017. It is now read-only.

Commit

Permalink
initial work on browser refresh middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jgallen23 committed Apr 5, 2012
1 parent 66a6416 commit a1882b9
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/browser-refresh/public/app.js
@@ -0,0 +1 @@
console.log('this is the app');
15 changes: 15 additions & 0 deletions examples/browser-refresh/server.js
@@ -0,0 +1,15 @@
var express = require('express');
var stalk = require('../../');

var port = 8001;
var app = express.createServer();

app.use(stalk.middleware([__dirname+'/public', __dirname+'/views']));

app.set('views', __dirname+'/views');
app.get('/', function(req, res){
res.render('index.jade', { layout: false });
});

console.log("Server started on port "+port);
app.listen(port);
6 changes: 6 additions & 0 deletions examples/browser-refresh/views/index.jade
@@ -0,0 +1,6 @@
!!!5
html
head
script(src='stalk.js')
body
p hi, this page will refresh when anything in views or public change
3 changes: 3 additions & 0 deletions index.js
@@ -0,0 +1,3 @@
var stalk = require('./lib/watch');
stalk.middleware = require('./lib/middleware');
module.exports = stalk;
44 changes: 44 additions & 0 deletions lib/browser-client.js
@@ -0,0 +1,44 @@
!function() {
/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08 **
** Code licensed under Creative Commons Attribution-ShareAlike License **
** http://creativecommons.org/licenses/by-sa/2.0/ **/
function XHConn()
{
var xmlhttp, bComplete = false;
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { try { xmlhttp = new XMLHttpRequest(); }
catch (e) { xmlhttp = false; }}}
if (!xmlhttp) return null;
this.xmlhttp = xmlhttp;
this.connect = function(sURL, fnDone)
{
if (!xmlhttp) return false;
bComplete = false;

xmlhttp.open('GET', sURL, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && !bComplete) {
bComplete = true;
fnDone(xmlhttp);
}
};
xmlhttp.send();
return xmlhttp;
};
return this;
}

var connect = function() {
var conn = new XHConn();
conn.connect('/stalk/', function(req) {
if (req.status === 200)
window.location.reload();
});
setTimeout(function() {
conn.xmlhttp.abort();
connect();
}, 1000 * 20);
};
connect();
}();
35 changes: 35 additions & 0 deletions lib/middleware.js
@@ -0,0 +1,35 @@
var fs = require('fs');
var watch = require('./watch');
var debug = require('debug')('stalk:middleware');

module.exports = function(dirs, options) {

var currentResponse = null;

watch(dirs, options, function() {
debug('changed');
if (currentResponse) {
try {
currentResponse.send({ refresh: true });
} catch(e) {
console.error('error sending response to browser, try manually refreshing the browser');
}
}
});

return function(req, res, next) {
if (req.url.match(/\/stalk.js/)) {
res.header('Content-Type', 'application/javascript');
fs.readFile(__dirname+'/browser-client.js', 'utf8', function(err, str) {
res.end(str);
});
} else if (req.url.match(/\/stalk\//)) {
debug('connected with browser');
currentResponse = res;
} else {
next();
}
};


};
1 change: 1 addition & 0 deletions lib/watch.js
Expand Up @@ -5,6 +5,7 @@ var path = require('path');


var defaults = {
ignore: []
};

var defaultIgnore = ['.git'];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -14,8 +14,9 @@
"debug": "*"
},
"devDependencies": {
"express": "*",
"jade": "*"
},
"main": "lib/watch.js",
"bin": { "stalk": "./bin/stalk.js" },
"keywords": ["watch", "cli"]
}

0 comments on commit a1882b9

Please sign in to comment.