This repository has been archived by the owner on Nov 27, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial work on browser refresh middleware
- Loading branch information
Showing
8 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('this is the app'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var stalk = require('./lib/watch'); | ||
stalk.middleware = require('./lib/middleware'); | ||
module.exports = stalk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}; | ||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ var path = require('path'); | |
|
||
|
||
var defaults = { | ||
ignore: [] | ||
}; | ||
|
||
var defaultIgnore = ['.git']; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters