Skip to content

Commit

Permalink
Use connect-body-rewrite.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenv committed Nov 14, 2013
1 parent 9dffe79 commit c68aa73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
31 changes: 11 additions & 20 deletions index.js
@@ -1,29 +1,20 @@
'use strict';

var rewrite = require('connect-body-rewrite');

module.exports = function (options) {
if (!options) {
options = {};
}
var host = options.host || "http://localhost:35729";
var script = "<script src=\"" + host + "/livereload.js?snipver=1\"></script>";

return function (req, res, next) {
var write = res.write;

res.write = function (chunk, encoding, callback) {
var length = res.getHeader('content-length');
var header = res.getHeader('content-type');

if (header && header.match(/text\/html/) && chunk) {
chunk = chunk.toString();
var origLength = chunk.length;
chunk = chunk.replace(/(<\/body>)/, script + "$1");
var newLength = chunk.length;
var script = "<script src=\"" + host + "/livereload.js?snipver=1\"></script>\n";

res.setHeader('content-length', length - (origLength - newLength));
}
write.call(res, chunk, encoding, callback);
};
next();
};
return rewrite({
accept: function (res) {
return res.getHeader('content-type').match(/text\/html/);
},
rewrite: function (body) {
return body.replace(/(<\/body>)/, script + "$1");
}
});
};
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -27,5 +27,8 @@
"repository": {
"type": "git",
"url": "git://github.com/rubenv/connect-livereload-safe.git"
},
"dependencies": {
"connect-body-rewrite": "0.0.1"
}
}

0 comments on commit c68aa73

Please sign in to comment.