Skip to content

Commit 324ca61

Browse files
coreyfarrellmcollina
authored andcommitted
Log warning if root directory does not exist instead of throwing error. (#102)
It is still an error of the root path exists but is not a directory. Fixes #101
1 parent b5c73bf commit 324ca61

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const send = require('send')
1111
const fp = require('fastify-plugin')
1212

1313
function fastifyStatic (fastify, opts, next) {
14-
const error = checkRootPathForErrors(opts.root)
14+
const error = checkRootPathForErrors(fastify, opts.root)
1515
if (error !== undefined) return next(error)
1616

1717
const setHeaders = opts.setHeaders
@@ -163,7 +163,7 @@ function fastifyStatic (fastify, opts, next) {
163163
next()
164164
}
165165

166-
function checkRootPathForErrors (rootPath) {
166+
function checkRootPathForErrors (fastify, rootPath) {
167167
if (rootPath === undefined) {
168168
return new Error('"root" option is required')
169169
}
@@ -180,7 +180,8 @@ function checkRootPathForErrors (rootPath) {
180180
pathStat = statSync(rootPath)
181181
} catch (e) {
182182
if (e.code === 'ENOENT') {
183-
return new Error(`"root" path "${rootPath}" must exist`)
183+
fastify.log.warn(`"root" path "${rootPath}" must exist`)
184+
return
184185
}
185186

186187
return e

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"devDependencies": {
3939
"@types/node": "^10.12.27",
40+
"concat-stream": "^2.0.0",
4041
"coveralls": "^3.0.3",
4142
"eslint-plugin-typescript": "^0.14.0",
4243
"fastify": "^2.0.0",

test/static.test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const t = require('tap')
88
const simple = require('simple-get')
99
const Fastify = require('fastify')
1010
const compress = require('fastify-compress')
11+
const concat = require('concat-stream')
12+
const pino = require('pino')
1113

1214
const fastifyStatic = require('../')
1315

@@ -592,14 +594,22 @@ t.test('prefix default', t => {
592594
t.doesNotThrow(() => fastify.register(fastifyStatic, pluginOptions))
593595
})
594596

595-
t.test('root not found error', t => {
596-
t.plan(1)
597+
t.test('root not found warning', t => {
598+
t.plan(2)
597599
const rootPath = path.join(__dirname, 'does-not-exist')
598600
const pluginOptions = { root: rootPath }
599-
const fastify = Fastify({ logger: false })
601+
const destination = concat(data => {
602+
t.equal(JSON.parse(data).msg, `"root" path "${rootPath}" must exist`)
603+
})
604+
const logger = pino({
605+
level: 'warn'
606+
}, destination)
607+
const fastify = Fastify({ logger: logger })
600608
fastify.register(fastifyStatic, pluginOptions)
601609
fastify.listen(0, err => {
602-
t.equal(err.message, `"root" path "${rootPath}" must exist`)
610+
t.error(err)
611+
fastify.server.unref()
612+
destination.end()
603613
})
604614
})
605615

@@ -673,7 +683,7 @@ t.test('setHeaders option', t => {
673683
})
674684

675685
t.test('errors', t => {
676-
t.plan(6)
686+
t.plan(5)
677687

678688
t.test('no root', t => {
679689
t.plan(1)
@@ -705,16 +715,6 @@ t.test('errors', t => {
705715
})
706716
})
707717

708-
t.test('root doesn\'t exist', t => {
709-
t.plan(1)
710-
const pluginOptions = { root: path.join(__dirname, 'foo', 'bar') }
711-
const fastify = Fastify({ logger: false })
712-
fastify.register(fastifyStatic, pluginOptions)
713-
.ready(err => {
714-
t.equal(err.constructor, Error)
715-
})
716-
})
717-
718718
t.test('root is not a directory', t => {
719719
t.plan(1)
720720
const pluginOptions = { root: __filename }

0 commit comments

Comments
 (0)