Skip to content

Commit 6fecbb5

Browse files
stutrekdaquinoaldo
authored andcommitted
Change exports to a factory (#7)
This makes the exports of the module a factory function rather than exporting app directly. Doing this brings the API in line with express and allows you to create more than one app in a node process. Thanks to @stutrek for his pull request #7 🥳
1 parent b50d108 commit 6fecbb5

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ npm i -s https-localhost
3131
```
3232
Then put in your `index.js` file:
3333
```
34-
const app = require("https-localhost")
34+
const httpsLocalhost = require("https-localhost")
35+
const app = httpLocalhost()
3536
// app is an express app, do what you usually do with express
3637
app.listen(port)
3738
```

index.js

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,69 @@ try {
2424
process.exit(1)
2525
}
2626

27-
// create a server with express
28-
const app = express()
27+
const createServer = () => {
28+
// create a server with express
29+
const app = express()
2930

30-
// override the default express listen method to use our server
31-
app.listen = function(port = process.env.PORT ||
32-
/* istanbul ignore next: cannot be tested on Travis */ 443) {
33-
app.server = https.createServer(certOptions, app).listen(port)
34-
console.info("Server running on port " + port + ".")
35-
return app.server
36-
}
31+
// override the default express listen method to use our server
32+
app.listen = function(port = process.env.PORT ||
33+
/* istanbul ignore next: cannot be tested on Travis */ 443) {
34+
app.server = https.createServer(certOptions, app).listen(port)
35+
console.info("Server running on port " + port + ".")
36+
return app.server
37+
}
3738

38-
// use gzip compression minify
39-
if (process.env.NODE_ENV === "production") {
40-
app.use(compression({ threshold: 1 }))
41-
app.use(minify())
42-
app.set("json spaces", 0)
43-
}
39+
// use gzip compression minify
40+
if (process.env.NODE_ENV === "production") {
41+
app.use(compression({ threshold: 1 }))
42+
app.use(minify())
43+
app.set("json spaces", 0)
44+
}
4445

45-
/* SETUP USEFUL FUNCTIONS */
46+
/* SETUP USEFUL FUNCTIONS */
4647

47-
// redirect http to https, usage `app.redirect()`
48-
app.redirect = function(
49-
/* istanbul ignore next: cannot be tested on Travis */ port = 80) {
50-
app.http = http.createServer((req, res) => {
51-
res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url })
52-
res.end()
53-
}).listen(port)
54-
console.info("http to https redirection active.")
55-
}
48+
// redirect http to https, usage `app.redirect()`
49+
app.redirect = function(
50+
/* istanbul ignore next: cannot be tested on Travis */ port = 80) {
51+
app.http = http.createServer((req, res) => {
52+
res.writeHead(301, {
53+
Location: "https://" + req.headers["host"] + req.url
54+
})
55+
res.end()
56+
}).listen(port)
57+
console.info("http to https redirection active.")
58+
}
59+
60+
// serve static content, usage `app.serve([path])`
61+
app.serve = function(staticPath = process.cwd(), port = process.env.PORT ||
62+
/* istanbul ignore next: cannot be tested on Travis */ 443) {
63+
app.use(express.static(staticPath))
64+
// redirect 404 to 404.html or to index.html
65+
app.use((req, res) => {
66+
if (!staticPath.startsWith("/"))
67+
staticPath = process.cwd() + "/" + staticPath
68+
const p404 = staticPath + "/404.html"
69+
const index = staticPath + "/index.html"
70+
// istanbul ignore else: not interesting
71+
if (fs.existsSync(p404))
72+
res.status(404).sendFile(p404)
73+
else if (fs.existsSync(index))
74+
res.status(404).sendFile(index)
75+
else res.status(404).send(req.path + " not found.")
76+
})
77+
console.info("Serving static path: " + staticPath)
78+
app.listen(port)
79+
}
5680

57-
// serve static content, usage `app.serve([path])`
58-
app.serve = function(staticPath = process.cwd(), port = process.env.PORT ||
59-
/* istanbul ignore next: cannot be tested on Travis */ 443) {
60-
app.use(express.static(staticPath))
61-
// redirect 404 to 404.html or to index.html
62-
app.use((req, res) => {
63-
if (!staticPath.startsWith("/"))
64-
staticPath = process.cwd() + "/" + staticPath
65-
const p404 = staticPath + "/404.html"
66-
const index = staticPath + "/index.html"
67-
// istanbul ignore else: not interesting
68-
if (fs.existsSync(p404))
69-
res.status(404).sendFile(p404)
70-
else if (fs.existsSync(index))
71-
res.status(404).sendFile(index)
72-
else res.status(404).send(req.path + " not found.")
73-
})
74-
console.info("Serving static path: " + staticPath)
75-
app.listen(port)
81+
return app
7682
}
7783

7884
/* MAIN */
7985

8086
// usage: `serve [<path>]` or `node index.js [<path>]`
8187
// istanbul ignore if: cannot be tested
8288
if (require.main === module) {
89+
const app = createServer()
8390
// retrieve the static path from the process argv or use the cwd
8491
// 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path
8592
app.serve(process.argv.length === 3 ? process.argv[2] : process.cwd())
@@ -88,4 +95,4 @@ if (require.main === module) {
8895
}
8996

9097
// export as module
91-
module.exports = app
98+
module.exports = createServer

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require("fs")
33
const http = require("http")
44
const https = require("https")
55

6-
const app = require("../index.js")
6+
const app = require("../index.js")()
77

88
const HTTPS_PORT = 4443
99
const HTTP_PORT = 8080
@@ -125,7 +125,7 @@ describe("Testing https-localhost", () => {
125125
// set NODE_ENV to production
126126
delete require.cache[require.resolve("../index.js")]
127127
process.env.NODE_ENV = "production"
128-
const production = require("../index.js")
128+
const production = require("../index.js")()
129129
// start the server (serving the test folder)
130130
production.serve("test", HTTPS_PORT)
131131
// make the request and check the output

0 commit comments

Comments
 (0)