diff --git a/README.md b/README.md index d196c3d..13f2644 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ npm i -s https-localhost ``` Then put in your `index.js` file: ``` -const app = require("https-localhost") +const httpsLocalhost = require("https-localhost") +const app = httpLocalhost() // app is an express app, do what you usually do with express app.listen(port) ``` diff --git a/index.js b/index.js index 4792197..d5fb907 100755 --- a/index.js +++ b/index.js @@ -24,55 +24,61 @@ try { process.exit(1) } -// create a server with express -const app = express() +const createServer = () => { + // create a server with express + const app = express() -// override the default express listen method to use our server -app.listen = function(port = process.env.PORT || -/* istanbul ignore next: cannot be tested on Travis */ 443) { - app.server = https.createServer(certOptions, app).listen(port) - console.info("Server running on port " + port + ".") - return app.server -} + // override the default express listen method to use our server + app.listen = function(port = process.env.PORT || + /* istanbul ignore next: cannot be tested on Travis */ 443) { + app.server = https.createServer(certOptions, app).listen(port) + console.info("Server running on port " + port + ".") + return app.server + } -// use gzip compression minify -if (process.env.NODE_ENV === "production") { - app.use(compression({ threshold: 1 })) - app.use(minify()) - app.set("json spaces", 0) -} + // use gzip compression minify + if (process.env.NODE_ENV === "production") { + app.use(compression({ threshold: 1 })) + app.use(minify()) + app.set("json spaces", 0) + } -/* SETUP USEFUL FUNCTIONS */ + /* SETUP USEFUL FUNCTIONS */ -// redirect http to https, usage `app.redirect()` -app.redirect = function( -/* istanbul ignore next: cannot be tested on Travis */ port = 80) { - app.http = http.createServer((req, res) => { - res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url }) - res.end() - }).listen(port) - console.info("http to https redirection active.") -} + // redirect http to https, usage `app.redirect()` + app.redirect = function( + /* istanbul ignore next: cannot be tested on Travis */ port = 80) { + app.http = http.createServer((req, res) => { + res.writeHead(301, { + Location: "https://" + req.headers["host"] + req.url + }) + res.end() + }).listen(port) + console.info("http to https redirection active.") + } + + // serve static content, usage `app.serve([path])` + app.serve = function(staticPath = process.cwd(), port = process.env.PORT || + /* istanbul ignore next: cannot be tested on Travis */ 443) { + app.use(express.static(staticPath)) + // redirect 404 to 404.html or to index.html + app.use((req, res) => { + if (!staticPath.startsWith("/")) + staticPath = process.cwd() + "/" + staticPath + const p404 = staticPath + "/404.html" + const index = staticPath + "/index.html" + // istanbul ignore else: not interesting + if (fs.existsSync(p404)) + res.status(404).sendFile(p404) + else if (fs.existsSync(index)) + res.status(404).sendFile(index) + else res.status(404).send(req.path + " not found.") + }) + console.info("Serving static path: " + staticPath) + app.listen(port) + } -// serve static content, usage `app.serve([path])` -app.serve = function(staticPath = process.cwd(), port = process.env.PORT || -/* istanbul ignore next: cannot be tested on Travis */ 443) { - app.use(express.static(staticPath)) - // redirect 404 to 404.html or to index.html - app.use((req, res) => { - if (!staticPath.startsWith("/")) - staticPath = process.cwd() + "/" + staticPath - const p404 = staticPath + "/404.html" - const index = staticPath + "/index.html" - // istanbul ignore else: not interesting - if (fs.existsSync(p404)) - res.status(404).sendFile(p404) - else if (fs.existsSync(index)) - res.status(404).sendFile(index) - else res.status(404).send(req.path + " not found.") - }) - console.info("Serving static path: " + staticPath) - app.listen(port) + return app } /* MAIN */ @@ -80,6 +86,7 @@ app.serve = function(staticPath = process.cwd(), port = process.env.PORT || // usage: `serve []` or `node index.js []` // istanbul ignore if: cannot be tested if (require.main === module) { + const app = createServer() // retrieve the static path from the process argv or use the cwd // 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path app.serve(process.argv.length === 3 ? process.argv[2] : process.cwd()) @@ -88,4 +95,4 @@ if (require.main === module) { } // export as module -module.exports = app +module.exports = createServer diff --git a/test/test.js b/test/test.js index b9a09d1..f6e3fab 100644 --- a/test/test.js +++ b/test/test.js @@ -3,7 +3,7 @@ const fs = require("fs") const http = require("http") const https = require("https") -const app = require("../index.js") +const app = require("../index.js")() const HTTPS_PORT = 4443 const HTTP_PORT = 8080 @@ -125,7 +125,7 @@ describe("Testing https-localhost", () => { // set NODE_ENV to production delete require.cache[require.resolve("../index.js")] process.env.NODE_ENV = "production" - const production = require("../index.js") + const production = require("../index.js")() // start the server (serving the test folder) production.serve("test", HTTPS_PORT) // make the request and check the output