Skip to content

Commit c590050

Browse files
chris-shaw-2011mcollina
authored andcommitted
Add rootPath overload parameter to sendFile function (#118)
1 parent 07b09b2 commit c590050

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ fastify.get('/another/path', function (req, reply) {
2424
reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
2525
})
2626

27+
fastify.get('/path/with/different/root', function (req, reply) {
28+
reply.sendFile('myHtml.html', path.join(__dirname, 'build')) // serving a file from a different root location
29+
})
30+
2731
```
2832

2933
### Options

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type HttpResponse = ServerResponse | Http2ServerResponse;
1313

1414
declare module "fastify" {
1515
interface FastifyReply<HttpResponse> {
16-
sendFile(filename: string): FastifyReply<HttpResponse>;
16+
sendFile(filename: string, rootPath?: string): FastifyReply<HttpResponse>;
1717
}
1818
}
1919

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ function fastifyStatic (fastify, opts, next) {
3333
maxAge: opts.maxAge
3434
}
3535

36-
function pumpSendToReply (request, reply, pathname) {
37-
const stream = send(request.raw, pathname, sendOptions)
36+
function pumpSendToReply (request, reply, pathname, rootPath) {
37+
var options = Object.assign({}, sendOptions)
38+
39+
if (rootPath) {
40+
options.root = rootPath
41+
}
42+
43+
const stream = send(request.raw, pathname, options)
3844
var resolvedFilename
3945
stream.on('file', function (file) {
4046
resolvedFilename = file
@@ -106,8 +112,8 @@ function fastifyStatic (fastify, opts, next) {
106112
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }
107113

108114
if (opts.decorateReply !== false) {
109-
fastify.decorateReply('sendFile', function (filePath) {
110-
pumpSendToReply(this.request, this, filePath)
115+
fastify.decorateReply('sendFile', function (filePath, rootPath) {
116+
pumpSendToReply(this.request, this, filePath, rootPath)
111117
})
112118
}
113119

test/static.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ t.test('serving disabled', t => {
514514
})
515515

516516
t.test('sendFile', t => {
517-
t.plan(2)
517+
t.plan(4)
518518

519519
const pluginOptions = {
520520
root: path.join(__dirname, '/static'),
@@ -527,6 +527,10 @@ t.test('sendFile', t => {
527527
reply.sendFile('/index.html')
528528
})
529529

530+
fastify.get('/root/path/override/test', (request, reply) => {
531+
reply.sendFile('/foo.html', path.join(__dirname, 'static', 'deep', 'path', 'for', 'test', 'purpose'))
532+
})
533+
530534
fastify.listen(0, err => {
531535
t.error(err)
532536

@@ -545,6 +549,34 @@ t.test('sendFile', t => {
545549
genericResponseChecks(t, response)
546550
})
547551
})
552+
553+
t.test('reply.sendFile() with rootPath', t => {
554+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
555+
simple.concat({
556+
method: 'GET',
557+
url: 'http://localhost:' + fastify.server.address().port + '/root/path/override/test',
558+
followRedirect: false
559+
}, (err, response, body) => {
560+
t.error(err)
561+
t.strictEqual(response.statusCode, 200)
562+
t.strictEqual(body.toString(), deepContent)
563+
genericResponseChecks(t, response)
564+
})
565+
})
566+
567+
t.test('reply.sendFile() again without root path', t => {
568+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
569+
simple.concat({
570+
method: 'GET',
571+
url: 'http://localhost:' + fastify.server.address().port + '/foo/bar',
572+
followRedirect: false
573+
}, (err, response, body) => {
574+
t.error(err)
575+
t.strictEqual(response.statusCode, 200)
576+
t.strictEqual(body.toString(), indexContent)
577+
genericResponseChecks(t, response)
578+
})
579+
})
548580
})
549581
})
550582

0 commit comments

Comments
 (0)