Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
97 changes: 52 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,69 @@ 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 */

// usage: `serve [<path>]` or `node index.js [<path>]`
// 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())
Expand All @@ -88,4 +95,4 @@ if (require.main === module) {
}

// export as module
module.exports = app
module.exports = createServer
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down