Skip to content

Commit f76710e

Browse files
committed
Map URLs to folders when no index is available
1 parent 22168ac commit f76710e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/resource-mapper.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,17 @@ class ResourceMapper {
6161
const files = await this._readdir(folder)
6262

6363
// Find a file with the same name (minus the dollar extension)
64-
const match = files.find(f => this._removeDollarExtension(f) === filename ||
64+
let match = files.find(f => this._removeDollarExtension(f) === filename ||
6565
(isIndex && f.startsWith(this._indexName + '.')))
6666
if (!match) {
67-
throw new Error(`File not found: ${fullPath}`)
67+
// Error if no match was found,
68+
// unless the URL ends with a '/',
69+
// in that case we fallback to the folder itself.
70+
if (isIndex) {
71+
match = '';
72+
} else {
73+
throw new Error(`File not found: ${fullPath}`)
74+
}
6875
}
6976
path = `${folder}${match}`
7077
contentType = this._getContentTypeByExtension(match)

test/unit/resource-mapper-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ describe('ResourceMapper', () => {
218218
contentType: 'text/html'
219219
})
220220

221+
itMapsUrl(mapper, 'a URL ending with a slash to a folder when no index is available',
222+
{
223+
url: 'http://localhost/space/'
224+
},
225+
{
226+
path: `${rootPath}space/`,
227+
contentType: 'application/octet-stream'
228+
})
229+
221230
itMapsUrl(mapper, 'a URL ending with a slash to an index file for text/html when index.html not is available',
222231
{
223232
url: 'http://localhost/space/',

0 commit comments

Comments
 (0)