Skip to content

Commit

Permalink
Added auto-redirection to ./index.html
Browse files Browse the repository at this point in the history
When a request is made with a URL that ends in /, Wire will automatically serve the index.html file in that directory (if it exists; otherwise, it will respond with a 404 error). An option to disable this feature will be added in the future.
This commit also adds the `$adjrequrl$` variable for use in custom error pages, which is replaced with the request URL with "index.html" appended if applicable (that is, if the original request has a URL ending in /). The default 404 error page has been updated to use this variable rather than `$requrl$`.
  • Loading branch information
ZelnickB committed Aug 23, 2020
1 parent a1fc3d7 commit e1bf307
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion defaults/errors/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
<h1>404 Error</h1>
<p>The requested resource $requrl$ was not found on this server. Please check the request URL.</p>
<p>The requested resource $adjrequrl$ was not found on this server. Please check the request URL.</p>
<p>
Error Message: $errmessage$<br />
Error Code: $errcode$<br />
Expand Down
13 changes: 11 additions & 2 deletions src/wire/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const errorPage = fs.readFileSync('../../defaults/errors/404.html', 'utf8') // D
const logo = fs.readFileSync('../../logo.svg', 'utf8')

function serverHandler (req, res) {
const filename = path.join(__dirname, '..', '..', '..', path.normalize(req.url))
let filename = path.join(__dirname, '..', '..', '..', path.normalize(req.url))
const mimetype = mimeTypes.lookup(filename)
const reqURLArray = req.url.split('/')
if (reqURLArray[1] === 'aluminum-internals') {
Expand All @@ -33,11 +33,20 @@ function serverHandler (req, res) {
return res.end()
}
}
if (reqURLArray[reqURLArray.length - 1] === '') {
filename += 'index.html'
}
fs.readFile(filename, function (err, data) {
if (err) {
let adjustedReqURL = req.url
if (reqURLArray[reqURLArray.length - 1] === '') {
adjustedReqURL += 'index.html'
}
res.writeHead(404, { 'Content-Type': 'html' })
res.write(
errorPage.replace(/\$requrl\$/g, req.url)
errorPage
.replace(/\$requrl\$/g, req.url)
.replace(/\$adjrequrl\$/g, adjustedReqURL)
.replace(/\$osplatform\$/g, os.platform())
.replace(/\$ostype\$/g, os.type())
.replace(/\$osversion\$/g, os.release())
Expand Down

0 comments on commit e1bf307

Please sign in to comment.