From b0e7f7e19717f2e3c9488e7e5e5a195c89fc9f2e Mon Sep 17 00:00:00 2001 From: brick <44xtc44@gmail.com> Date: Wed, 4 Feb 2026 17:44:19 +0100 Subject: [PATCH] issue: Add the example code using ESM #544 (plugin tests already in fastify's /test/esm) --- README.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 770cf68..22e5952 100644 --- a/README.md +++ b/README.md @@ -29,14 +29,26 @@ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Ref ## Usage ```js -const fastify = require('fastify')({logger: true}) -const path = require('node:path') - -fastify.register(require('@fastify/static'), { - root: path.join(__dirname, 'public'), - prefix: '/public/', // optional: default '/' - constraints: { host: 'example.com' } // optional: default {} -}) +// ESM +import path from "path"; +import Fastify from "fastify"; +const fastify = Fastify({ logger: true }); + +fastify.register(import("@fastify/static"), { + root: path.join(__dirname, "public"), + prefix: "/public/", // optional: default '/' + constraints: { host: "example.com" }, // optional: default {} +}); + +// CommonJs +const fastify = require("fastify")({ logger: true }); +const path = require("node:path"); + +fastify.register(require("@fastify/static"), { + root: path.join(__dirname, "public"), + prefix: "/public/", // optional: default '/' + constraints: { host: "example.com" }, // optional: default {} +}); fastify.get('/another/path', function (req, reply) { reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly