Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove return from sync route handlers examples #333

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Fdawgs marked this conversation as resolved.
Show resolved Hide resolved
})

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