allow serving express app

This commit is contained in:
AJ ONeal 2016-08-06 12:34:15 -06:00
parent bcd927b7ea
commit b9edabe4bf
3 changed files with 23 additions and 5 deletions

View File

@ -26,6 +26,7 @@ Usage
* `-p <port>` - i.e. `sudo serve-https -p 443` (defaults to 8443) * `-p <port>` - i.e. `sudo serve-https -p 443` (defaults to 8443)
* `-d <dirpath>` - i.e. `serve-https -d /tmp/` (defaults to `pwd`) * `-d <dirpath>` - i.e. `serve-https -d /tmp/` (defaults to `pwd`)
* `-c <content>` - i.e. `server-https -c 'Hello, World! '` (defaults to directory index) * `-c <content>` - i.e. `server-https -c 'Hello, World! '` (defaults to directory index)
* `--express-app` - path to a file the exports an express-style app (`function (req, res, next) { ... }`)
* `--livereload` - inject livereload into all html pages (see also: [fswatch](http://stackoverflow.com/a/13807906/151312)) * `--livereload` - inject livereload into all html pages (see also: [fswatch](http://stackoverflow.com/a/13807906/151312))
* `--insecure-port <port>` - run an http server that redirects to https (off by default) * `--insecure-port <port>` - run an http server that redirects to https (off by default)

10
app.js
View File

@ -46,7 +46,15 @@ module.exports = function (opts) {
}; };
serve(req, res, function (err) { serve(req, res, function (err) {
if (err) { return done(err); } if (err) { return done(err); }
index(req, res, done); index(req, res, function (err) {
if (err) { return done(err); }
if (opts.expressApp) {
opts.expressApp(req, res, done);
}
else {
done();
}
});
}); });
}; };
}; };

View File

@ -32,7 +32,8 @@ function createServer(port, pubdir, content, opts) {
var server = https.createServer(opts); var server = https.createServer(opts);
var app = require('./app'); var app = require('./app');
var directive = { public: pubdir, content: content, livereload: opts.livereload, servername: opts.servername }; var directive = { public: pubdir, content: content, livereload: opts.livereload
, servername: opts.servername, expressApp: opts.expressApp };
var livereload = require('livereload'); var livereload = require('livereload');
var server2 = livereload.createServer({ https: opts }); var server2 = livereload.createServer({ https: opts });
@ -48,12 +49,15 @@ function createServer(port, pubdir, content, opts) {
}); });
server.listen(port, function () { server.listen(port, function () {
var msg = 'Serving ' + pubdir + ' at https://' + opts.servername; var msg = 'Serving ' + pubdir + ' at ';
var httpsUrl = 'https://' + opts.servername;
var p = server.address().port; var p = server.address().port;
if (443 !== p) { if (443 !== p) {
msg += ':' + p; httpsUrl += ':' + p;
} }
console.info(msg); console.info(msg);
console.info('');
console.info('\t' + httpsUrl);
}); });
if ('function' === typeof app) { if ('function' === typeof app) {
@ -62,7 +66,7 @@ function createServer(port, pubdir, content, opts) {
app = app.create(directive); app = app.create(directive);
} }
Promise.resolve(app).then(function (app) { return Promise.resolve(app).then(function (app) {
server.on('request', app); server.on('request', app);
}); });
} }
@ -96,6 +100,7 @@ function run() {
argv.root = argv.root || argv.chain || ''; argv.root = argv.root || argv.chain || '';
argv.servername = argv.servername || letsencryptHost; argv.servername = argv.servername || letsencryptHost;
argv['serve-root'] = argv['serve-root'] || argv['serve-chain']; argv['serve-root'] = argv['serve-root'] || argv['serve-chain'];
// argv[express-app]
} }
if (argv['serve-root'] && !argv.root) { if (argv['serve-root'] && !argv.root) {
@ -150,6 +155,10 @@ function run() {
opts.insecurePort = argv.i || argv['insecure-port']; opts.insecurePort = argv.i || argv['insecure-port'];
opts.livereload = livereload; opts.livereload = livereload;
if (argv['express-app']) {
opts.expressApp = require(path.resolve(process.cwd(), argv['express-app']));
}
createServer(port, pubdir, content, opts); createServer(port, pubdir, content, opts);
} }