Skip to content

Commit

Permalink
chore: remove return from sync route handlers examples (#333)
Browse files Browse the repository at this point in the history
* chore: remove `return` from sync route handlers

* Update README.md

Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>

Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>
  • Loading branch information
Fdawgs and Eomm authored Aug 16, 2022
1 parent 9dad9b0 commit 3987e39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ fastify.register(require('@fastify/static'), {
})

fastify.get('/another/path', function (req, reply) {
return reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
})

fastify.get('another/patch-async', async function (req, reply) {
return reply.sendFile('myHtml.html')
})

fastify.get('/path/with/different/root', function (req, reply) {
return reply.sendFile('myHtml.html', path.join(__dirname, 'build')) // serving a file from a different root location
reply.sendFile('myHtml.html', path.join(__dirname, 'build')) // serving a file from a different root location
})

fastify.get('/another/path', function (req, reply) {
return reply.sendFile('myHtml.html', { cacheControl: false }) // overriding the options disabling cache-control headers
reply.sendFile('myHtml.html', { cacheControl: false }) // overriding the options disabling cache-control headers
})
```

Expand Down Expand Up @@ -69,15 +73,20 @@ fastify.register(require('@fastify/static'), {
})

fastify.get('/another/path', function (req, reply) {
return reply.download('myHtml.html', 'custom-filename.html') // sending path.join(__dirname, 'public', 'myHtml.html') directly with custom filename
reply.download('myHtml.html', 'custom-filename.html') // sending path.join(__dirname, 'public', 'myHtml.html') directly with custom filename
})

fastify.get('another/patch-async', async function (req, reply) {
// an async handler must always return the reply object
return reply.download('myHtml.html', 'custom-filename.html')
})

fastify.get('/path/without/cache/control', function (req, reply) {
return reply.download('myHtml.html', { cacheControl: false }) // serving a file disabling cache-control headers
reply.download('myHtml.html', { cacheControl: false }) // serving a file disabling cache-control headers
})

fastify.get('/path/without/cache/control', function (req, reply) {
return reply.download('myHtml.html', 'custom-filename.html', { cacheControl: false })
reply.download('myHtml.html', 'custom-filename.html', { cacheControl: false })
})

```
Expand Down
20 changes: 10 additions & 10 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ t.test('serving disabled', (t) => {
fastify.register(fastifyStatic, pluginOptions)

fastify.get('/foo/bar', (request, reply) => {
return reply.sendFile('index.html')
reply.sendFile('index.html')
})

t.teardown(fastify.close.bind(fastify))
Expand Down Expand Up @@ -856,18 +856,18 @@ t.test('sendFile', (t) => {
fastify.register(fastifyStatic, pluginOptions)

fastify.get('/foo/bar', function (req, reply) {
return reply.sendFile('/index.html')
reply.sendFile('/index.html')
})

fastify.get('/root/path/override/test', (request, reply) => {
return reply.sendFile(
reply.sendFile(
'/foo.html',
path.join(__dirname, 'static', 'deep', 'path', 'for', 'test', 'purpose')
)
})

fastify.get('/foo/bar/options/override/test', function (req, reply) {
return reply.sendFile('/index.html', { maxAge })
reply.sendFile('/index.html', { maxAge })
})

fastify.listen({ port: 0 }, (err) => {
Expand Down Expand Up @@ -1066,26 +1066,26 @@ t.test('download', (t) => {
fastify.register(fastifyStatic, pluginOptions)

fastify.get('/foo/bar', function (req, reply) {
return reply.download('/index.html')
reply.download('/index.html')
})

fastify.get('/foo/bar/change', function (req, reply) {
return reply.download('/index.html', 'hello-world.html')
reply.download('/index.html', 'hello-world.html')
})

fastify.get('/foo/bar/override', function (req, reply) {
return reply.download('/index.html', 'hello-world.html', {
reply.download('/index.html', 'hello-world.html', {
maxAge: '2 hours',
immutable: true
})
})

fastify.get('/foo/bar/override/2', function (req, reply) {
return reply.download('/index.html', { acceptRanges: false })
reply.download('/index.html', { acceptRanges: false })
})

fastify.get('/root/path/override/test', (request, reply) => {
return reply.download('/foo.html', {
reply.download('/foo.html', {
root: path.join(
__dirname,
'static',
Expand All @@ -1099,7 +1099,7 @@ t.test('download', (t) => {
})

fastify.get('/root/path/override/test/change', (request, reply) => {
return reply.download('/foo.html', 'hello-world.html', {
reply.download('/foo.html', 'hello-world.html', {
root: path.join(
__dirname,
'static',
Expand Down

0 comments on commit 3987e39

Please sign in to comment.