From 85a32486e127e1c9a38e736ea34015e524451041 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 30 Jun 2015 17:11:01 -0600 Subject: [PATCH] add ability to specify content for / --- README.md | 23 ++++++++++++++++++++++- app.js | 6 ++++++ serve.js | 15 +++++++++------ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b3a192a..7642e15 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# localhost.daplie.com-server +localhost.daplie.com-server +=========================== + Serves HTTPS using TLS (SSL) certs for localhost.daplie.com - great for testing and development. Install @@ -18,3 +20,22 @@ Usage * `-p ` - i.e. `sudo serve-https -p 443` * `-d ` - i.e. `serve-https -d /tmp/` +* `-c ` - i.e. `server-https -c 'Hello, World!'` + +Examples +-------- + +```bash +serve-https -p 1443 -c 'Hello from 1443' & +serve-https -p 2443 -c 'Hello from 2443' & +serve-https -p 3443 -d /tmp & + +curl https://localhost.daplie.com:1443 +> Hello from 1443 + +curl --insecure https://localhost:2443 +> Hello from 2443 + +curl https://localhost.daplie.com:3443 +> [html index listing of /tmp] +``` diff --git a/app.js b/app.js index e6a9f8d..ed7ca78 100644 --- a/app.js +++ b/app.js @@ -6,8 +6,14 @@ module.exports = function (opts) { var serveIndex = require('serve-index'); var serve = serveStatic(opts.public); var index = serveIndex(opts.public); + var content = opts.content; return function (req, res) { + if (content && '/' === req.url) { + // res.setHeader('Content-Type', 'application/octet-stream'); + res.end(content); + return; + } var done = finalhandler(req, res); serve(req, res, function (err) { if (err) { return done(err); } diff --git a/serve.js b/serve.js index 6a93434..34ab234 100755 --- a/serve.js +++ b/serve.js @@ -4,9 +4,11 @@ var https = require('https'); var path = require('path'); -function createServer(port, pubdir) { - var server = https.createServer(require('localhost.daplie.com-certificates')); +function createServer(port, pubdir, content) { + var options = require('localhost.daplie.com-certificates'); + var server = https.createServer(options); var app = require('./app'); + var directive = { public: pubdir, content: content }; server.on('error', function (err) { console.error(err); @@ -19,13 +21,13 @@ function createServer(port, pubdir) { if (443 !== p) { msg += ':' + p; } - console.log(msg); + console.info(msg); }); if ('function' === typeof app) { - app = app({ public: pubdir }); + app = app(directive); } else if ('function' === typeof app.create) { - app = app.create({ public: pubdir }); + app = app.create(directive); } Promise.resolve(app).then(function (app) { @@ -40,7 +42,8 @@ function run() { var argv = minimist(process.argv.slice(2)); var port = argv.p || argv._[0] || 1443; var pubdir = path.resolve(argv.d || argv._[1] || process.cwd()); - createServer(port, pubdir); + var content = argv.c; + createServer(port, pubdir, content); } if (require.main === module) {