Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
14 additions
and
20 deletions.
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 |
---|---|---|
@@ -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"); | ||
} | ||
}); | ||
}; |
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