From 11555ba76189b2306bb6993eaae87df055d405e0 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 24 Jun 2015 15:36:17 -0600 Subject: [PATCH] initial commit --- app.js | 17 +++++++++++++++++ package.json | 44 ++++++++++++++++++++++++++++++++++++++++++++ serve.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 app.js create mode 100644 package.json create mode 100644 serve.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..e6a9f8d --- /dev/null +++ b/app.js @@ -0,0 +1,17 @@ +'use strict'; + +module.exports = function (opts) { + var finalhandler = require('finalhandler'); + var serveStatic = require('serve-static'); + var serveIndex = require('serve-index'); + var serve = serveStatic(opts.public); + var index = serveIndex(opts.public); + + return function (req, res) { + var done = finalhandler(req, res); + serve(req, res, function (err) { + if (err) { return done(err); } + index(req, res, done); + }); + }; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8991288 --- /dev/null +++ b/package.json @@ -0,0 +1,44 @@ +{ + "name": "https-server", + "version": "1.0.0", + "description": "Serves HTTPS using TLS (SSL) certs for localhost.daplie.com - great for testing and development.", + "main": "serve.js", + "scripts": { + "test": "node serve.js -p 1443 -d /tmp/" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Daplie/localhost.daplie.com-server.git" + }, + "keywords": [ + "https", + "local", + "localhost", + "development", + "dev", + "tls", + "ssl", + "cert", + "certs", + "certificate", + "certificates", + "http", + "express", + "connect", + "serve", + "server" + ], + "author": "AJ ONeal (http://coolaj86.com/)", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/Daplie/localhost.daplie.com-server/issues" + }, + "homepage": "https://github.com/Daplie/localhost.daplie.com-server#readme", + "dependencies": { + "finalhandler": "^0.4.0", + "localhost.daplie.com-certificates": "^1.0.0", + "minimist": "^1.1.1", + "serve-index": "^1.7.0", + "serve-static": "^1.10.0" + } +} diff --git a/serve.js b/serve.js new file mode 100644 index 0000000..dcf5c95 --- /dev/null +++ b/serve.js @@ -0,0 +1,47 @@ +'use strict'; + +var https = require('https'); +var path = require('path'); + +function createServer(port, pubdir) { + var server = https.createServer(require('localhost.daplie.com-certificates')); + var app = require('./app'); + + server.on('error', function (err) { + console.error(err); + process.exit(1); + }); + + server.listen(port, function () { + var msg = 'Serving ' + pubdir + ' at https://localhost.daplie.com'; + var p = server.address().port; + if (443 !== p) { + msg += ':' + p; + } + console.log(msg); + }); + + if ('function' === typeof app) { + app = app({ public: pubdir }); + } else if ('function' === typeof app.create) { + app = app.create({ public: pubdir }); + } + + Promise.resolve(app).then(function (app) { + server.on('request', app); + }); +} + +module.exports.createServer = createServer; + +function run() { + var minimist = require('minimist'); + 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); +} + +if (require.main === module) { + run(); +}