Skip to content

Commit dea5c4e

Browse files
jsardevmcollina
authored andcommitted
add an option to disable serving (#69)
resolves #20
1 parent 5423247 commit dea5c4e

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ The following options are also supported and will be passed directly to the
7171
- [`lastModified`](https://www.npmjs.com/package/send#lastmodified)
7272
- [`maxAge`](https://www.npmjs.com/package/send#maxage)
7373

74+
#### Disable serving
75+
76+
If you'd just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option `{ serve: false }`. This will prevent the plugin from serving everything under `root`.
77+
7478
#### Disabling reply decorator
7579

7680
The reply object is decorated with a `sendFile` function by default. If you want to

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ function fastifyStatic (fastify, opts, next) {
9191
// Set the schema hide property if defined in opts or true by default
9292
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }
9393

94-
fastify.get(prefix + '*', schema, function (req, reply) {
95-
pumpSendToReply(req, reply, '/' + req.params['*'])
96-
})
94+
if (opts.serve !== false) {
95+
fastify.get(prefix + '*', schema, function (req, reply) {
96+
pumpSendToReply(req, reply, '/' + req.params['*'])
97+
})
98+
}
9799

98100
if (opts.decorateReply !== false) {
99101
fastify.decorateReply('sendFile', function (filePath) {

test/static.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,54 @@ t.test('not found responses can be customized with fastify.setNotFoundHandler()'
435435
})
436436
})
437437

438+
t.test('serving disabled', t => {
439+
t.plan(3)
440+
441+
const pluginOptions = {
442+
root: path.join(__dirname, '/static'),
443+
prefix: '/static/',
444+
serve: false
445+
}
446+
const fastify = Fastify()
447+
fastify.register(fastifyStatic, pluginOptions)
448+
449+
fastify.get('/foo/bar', (request, reply) => {
450+
reply.sendFile('index.html')
451+
})
452+
453+
t.tearDown(fastify.close.bind(fastify))
454+
455+
fastify.listen(0, err => {
456+
t.error(err)
457+
458+
fastify.server.unref()
459+
460+
t.test('/static/index.html not found', t => {
461+
t.plan(2)
462+
simple.concat({
463+
method: 'GET',
464+
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
465+
}, (err, response, body) => {
466+
t.error(err)
467+
t.strictEqual(response.statusCode, 404)
468+
})
469+
})
470+
471+
t.test('/static/index.html via sendFile found', t => {
472+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
473+
simple.concat({
474+
method: 'GET',
475+
url: 'http://localhost:' + fastify.server.address().port + '/foo/bar'
476+
}, (err, response, body) => {
477+
t.error(err)
478+
t.strictEqual(response.statusCode, 200)
479+
t.strictEqual(body.toString(), indexContent)
480+
genericResponseChecks(t, response)
481+
})
482+
})
483+
})
484+
})
485+
438486
t.test('sendFile', t => {
439487
t.plan(2)
440488

0 commit comments

Comments
 (0)