add livereload

This commit is contained in:
AJ ONeal 2015-12-05 22:43:33 -08:00
parent f344ee591d
commit 9a264c4771
4 changed files with 44 additions and 1 deletions

View File

@ -26,6 +26,7 @@ Usage
* `-p <port>` - i.e. `sudo serve-https -p 443` (defaults to 8443)
* `-d <dirpath>` - i.e. `serve-https -d /tmp/` (defaults to `pwd`)
* `-c <content>` - i.e. `server-https -c 'Hello, World! '` (defaults to directory index)
* `--livereload` - inject livereload into all html pages
* `--insecure-port <port>` - run an http server that redirects to https (off by default)
Specifying a custom HTTPS certificate:

34
app.js
View File

@ -15,6 +15,40 @@ module.exports = function (opts) {
return;
}
var done = finalhandler(req, res);
var livereload = '';
var addLen = 0;
if (opts.livereload) {
livereload = '<script src="//'
+ (res.getHeader('Host') || opts.servername).split(':')[0]
+ ':35729/livereload.js?snipver=1"></script>';
addLen = livereload.length;
}
res.__end = res.end;
res.__write = res.write;
res.write = function (data, enc, cb) {
if (this.headersSent) {
return;
}
if (!/html/i.test(this.getHeader('Content-Type'))) {
this.__write(data, enc, cb);
return;
}
if (this.getHeader('Content-Length')) {
this.setHeader('Content-Length', this.getHeader('Content-Length') + addLen);
}
this.__write(livereload);
this.__write(data, enc, cb);
};
res.end = function () {
res.__end();
};
serve(req, res, function (err) {
if (err) { return done(err); }
index(req, res, done);

View File

@ -39,6 +39,7 @@
"homepage": "https://github.com/Daplie/localhost.daplie.com-server#readme",
"dependencies": {
"finalhandler": "^0.4.0",
"livereload": "^0.4.0",
"localhost.daplie.com-certificates": "^1.1.2",
"minimist": "^1.1.1",
"redirect-https": "^1.1.0",

View File

@ -31,7 +31,12 @@ function createInsecureServer(port, pubdir, opts) {
function createServer(port, pubdir, content, opts) {
var server = https.createServer(opts);
var app = require('./app');
var directive = { public: pubdir, content: content };
var directive = { public: pubdir, content: content, livereload: opts.livereload, servername: opts.servername };
var livereload = require('livereload');
var server2 = livereload.createServer({ https: opts });
server2.watch(pubdir);
if (opts.insecurePort) {
createInsecureServer(port, pubdir, opts);
@ -68,6 +73,7 @@ function run() {
var minimist = require('minimist');
var argv = minimist(process.argv.slice(2));
var port = argv.p || argv.port || argv._[0] || 8443;
var livereload = argv.livereload;
var pubdir = path.resolve(argv.d || argv._[1] || process.cwd());
var content = argv.c;
var letsencryptHost = argv['letsencrypt-certs'];
@ -142,6 +148,7 @@ function run() {
opts.servername = argv.servername;
}
opts.insecurePort = argv.i || argv['insecure-port'];
opts.livereload = livereload;
createServer(port, pubdir, content, opts);
}