Skip to content

Commit b50d108

Browse files
committed
Search for a 404 template
When serving a static path, in case of 404 error instead of print the error to the user, look for a 404.html template or index.html. If none of them is founded the error is printed. Also, remove tests for installation, to avoid waste on developers devices. It is not necessari to be tested, since if npm install fails on Travis, the entire test fails.
1 parent 63eef46 commit b50d108

File tree

5 files changed

+97
-70
lines changed

5 files changed

+97
-70
lines changed

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,23 @@ app.redirect = function(
5555
}
5656

5757
// serve static content, usage `app.serve([path])`
58-
app.serve = function(path = process.cwd(), port = process.env.PORT ||
58+
app.serve = function(staticPath = process.cwd(), port = process.env.PORT ||
5959
/* istanbul ignore next: cannot be tested on Travis */ 443) {
60-
app.use(express.static(path))
61-
console.info("Serving static path: " + path)
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)
6275
app.listen(port)
6376
}
6477

package-lock.json

Lines changed: 52 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "https-localhost",
3-
"version": "3.1.2",
3+
"version": "3.2.0",
44
"description": "HTTPS server running on localhost",
55
"main": "index.js",
66
"scripts": {
@@ -36,14 +36,14 @@
3636
},
3737
"homepage": "https://daquinoaldo.github.io/https-localhost",
3838
"dependencies": {
39-
"compression": "^1.7.3",
39+
"compression": "^1.7.4",
4040
"express": "^4.16.4",
4141
"express-minify": "^1.0.0",
4242
"spdy": "^4.0.0"
4343
},
4444
"devDependencies": {
4545
"coveralls": "^3.0.3",
46-
"eslint": "^5.14.1",
46+
"eslint": "^5.15.3",
4747
"eslint-config-standard": "^12.0.0",
4848
"eslint-plugin-import": "^2.16.0",
4949
"eslint-plugin-node": "^8.0.1",

test/404.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>404</title>
4+
</head>
5+
<body>
6+
<p>File not found.</p>
7+
</body>
8+
</html>

test/test.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,6 @@ function makeRequest(path = "/", secure = true, port = HTTPS_PORT) {
3434
})
3535
}
3636

37-
// TESTS INSTALL
38-
describe("Testing the installation script", function() {
39-
// timeout 5 min
40-
this.timeout(300000)
41-
42-
// remove a file, this will force the reinstallation
43-
fs.unlinkSync("cert/localhost.crt")
44-
45-
it("installs correctly", async function() {
46-
await require("../cert/generate.js")()
47-
assert(fs.existsSync("cert/localhost.crt"))
48-
assert(fs.existsSync("cert/localhost.key"))
49-
})
50-
51-
it("skips installation if files exists", async function() {
52-
await require("../cert/generate.js")()
53-
assert(fs.existsSync("cert/localhost.crt"))
54-
assert(fs.existsSync("cert/localhost.key"))
55-
})
56-
})
57-
5837
// TESTS MODULE
5938
describe("Testing https-localhost", () => {
6039
// close the server after each test
@@ -91,15 +70,15 @@ describe("Testing https-localhost", () => {
9170
res.data.toString() === fs.readFileSync("test/static.html").toString()))
9271
})
9372

94-
it("serves static files from default path and env port", async function() {
73+
it("serves static files from default env port", async function() {
9574
// set the environment port
9675
process.env.PORT = HTTPS_PORT
9776
// start the server (serving the default folder)
98-
app.serve()
77+
app.serve("test")
9978
// make the request and check the output
100-
await makeRequest("/test/static.html")
101-
.then(res => assert(
102-
res.data.toString() === fs.readFileSync("test/static.html").toString()))
79+
await makeRequest("/static.html")
80+
.then(res => assert(res.data.toString() ===
81+
fs.readFileSync("test/static.html").toString()))
10382
})
10483

10584
it("doesn't crash on 404", async function() {
@@ -110,6 +89,19 @@ describe("Testing https-localhost", () => {
11089
.then(res => assert(res.statusCode === 404))
11190
})
11291

92+
it("looks for a 404.html file", async function() {
93+
// start the server (serving the default folder)
94+
await app.serve("test", HTTPS_PORT)
95+
// make the request and check the result
96+
await makeRequest("/do-not-exist.html")
97+
.then(res => {
98+
console.log(res)
99+
assert(res.statusCode === 404)
100+
assert(res.data.toString() ===
101+
fs.readFileSync("test/404.html").toString())
102+
})
103+
})
104+
113105
it("doesn't crash if the static path doesn't exists", async function() {
114106
// start the server (serving a non existing folder)
115107
app.serve("do-not-exists")

0 commit comments

Comments
 (0)