add ability to specify content for /

This commit is contained in:
AJ ONeal 2015-06-30 17:11:01 -06:00
parent d7b9cf55da
commit 85a32486e1
3 changed files with 37 additions and 7 deletions

View File

@ -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. Serves HTTPS using TLS (SSL) certs for localhost.daplie.com - great for testing and development.
Install Install
@ -18,3 +20,22 @@ Usage
* `-p <port>` - i.e. `sudo serve-https -p 443` * `-p <port>` - i.e. `sudo serve-https -p 443`
* `-d <dirpath>` - i.e. `serve-https -d /tmp/` * `-d <dirpath>` - i.e. `serve-https -d /tmp/`
* `-c <content>` - 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]
```

6
app.js
View File

@ -6,8 +6,14 @@ module.exports = function (opts) {
var serveIndex = require('serve-index'); var serveIndex = require('serve-index');
var serve = serveStatic(opts.public); var serve = serveStatic(opts.public);
var index = serveIndex(opts.public); var index = serveIndex(opts.public);
var content = opts.content;
return function (req, res) { return function (req, res) {
if (content && '/' === req.url) {
// res.setHeader('Content-Type', 'application/octet-stream');
res.end(content);
return;
}
var done = finalhandler(req, res); var done = finalhandler(req, res);
serve(req, res, function (err) { serve(req, res, function (err) {
if (err) { return done(err); } if (err) { return done(err); }

View File

@ -4,9 +4,11 @@
var https = require('https'); var https = require('https');
var path = require('path'); var path = require('path');
function createServer(port, pubdir) { function createServer(port, pubdir, content) {
var server = https.createServer(require('localhost.daplie.com-certificates')); var options = require('localhost.daplie.com-certificates');
var server = https.createServer(options);
var app = require('./app'); var app = require('./app');
var directive = { public: pubdir, content: content };
server.on('error', function (err) { server.on('error', function (err) {
console.error(err); console.error(err);
@ -19,13 +21,13 @@ function createServer(port, pubdir) {
if (443 !== p) { if (443 !== p) {
msg += ':' + p; msg += ':' + p;
} }
console.log(msg); console.info(msg);
}); });
if ('function' === typeof app) { if ('function' === typeof app) {
app = app({ public: pubdir }); app = app(directive);
} else if ('function' === typeof app.create) { } else if ('function' === typeof app.create) {
app = app.create({ public: pubdir }); app = app.create(directive);
} }
Promise.resolve(app).then(function (app) { Promise.resolve(app).then(function (app) {
@ -40,7 +42,8 @@ function run() {
var argv = minimist(process.argv.slice(2)); var argv = minimist(process.argv.slice(2));
var port = argv.p || argv._[0] || 1443; var port = argv.p || argv._[0] || 1443;
var pubdir = path.resolve(argv.d || argv._[1] || process.cwd()); 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) { if (require.main === module) {