Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ send.mime.default_type = 'application/octet-stream'

/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions>} */
async function fastifyStatic (fastify, opts) {
opts.root = normalizeRoot(opts.root)
checkRootPathForErrors(fastify, opts.root)
if (opts.serve !== false) {
opts.root = normalizeRoot(opts.root)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dipali127
Why should normalizeRoot also be skipped?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this scenario we are not using root if I'm not wrong

checkRootPathForErrors(fastify, opts.root)
}

const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
Expand Down
11 changes: 11 additions & 0 deletions test/root/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test file</title>
</head>
<body>
<h1>hello fastify</h1>
</body>
</html>
40 changes: 40 additions & 0 deletions test/serve-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const path = require('node:path')
const assert = require('node:assert')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifyStatic = require('../index.js')

test('should not serve static files when serve is false', async t => {
const fastify = Fastify()
fastify.register(fastifyStatic, {
serve: false
})

t.after(() => fastify.close())
await fastify.listen({ port: 0 })
console.log('Server running at http://localhost:0')
fastify.server.unref()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fastify.server.unref()
t.after(() => fastify.close())


const res = await fetch('http://localhost:' + fastify.server.address().port + '/public/example.html')
assert.strictEqual(res.status, 404);

Check failure on line 21 in test/serve-option.test.js

View workflow job for this annotation

GitHub Actions / test / quality-check / Lint Code

Extra semicolon
})

test('should serve static files when serve is true', async t => {
const fastify = Fastify()
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'root'),
prefix: '/public/'
})

t.after(() => fastify.close())
await fastify.listen({ port: 0 })
fastify.server.unref()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fastify.server.unref()
t.after(() => fastify.close())


const res = await fetch('http://localhost:' + fastify.server.address().port + '/public/example.html')
assert.strictEqual(res.status, 200);

Check failure on line 36 in test/serve-option.test.js

View workflow job for this annotation

GitHub Actions / test / quality-check / Lint Code

Extra semicolon

const content = await res.text()
assert.ok(content.includes('hello'), 'File content should contain "hello"');

Check failure on line 39 in test/serve-option.test.js

View workflow job for this annotation

GitHub Actions / test / quality-check / Lint Code

Extra semicolon
})
Loading