Fastify
Fastify is a web framework for Node.js designed for high performance and low overhead. It features a powerful plugin architecture, built-in schema-based validation, and asynchronous request handling, enabling fast and efficient APIs. Its focus on speed, extensibility, and developer-friendly tooling makes it suitable for both small projects and large-scale production applications.
Example Use
This example assumes you are familiar with the @zeroad.network/token module, its use cases, and installation process. If not, please review the Node, Bun, and Deno page first.
The following demonstrates how to use the module with the fastify framework. It serves as a reference and starting point. Full examples are available in the examples section of our GitHub repository:
import Fastify from "fastify";
import { Site, FEATURE } from "@zeroad.network/token";
/**
* Module initialization (once at startup)
*/
const site = Site({
// Pass in `clientId` you received registering your site on Zero Ad Network platform
clientId: "Z2CclA8oXIT1e0QmqTWF8w",
// Specify supported site features only
features: [FEATURE.CLEAN_WEB, FEATURE.ONE_PASS],
});
// -----------------------------------------------------------------------------
// Fastify app setup
// -----------------------------------------------------------------------------
const app = Fastify();
// Extend FastifyRequest interface to include tokenContext
declare module "fastify" {
interface FastifyRequest {
tokenContext: ReturnType<typeof site.parseClientToken>;
}
}
// -----------------------------------------------------------------------------
// Middleware (Fastify hook)
// -----------------------------------------------------------------------------
app.addHook("onRequest", async (request, reply) => {
// Inject the "X-Better-Web-Welcome" server header into every response
reply.header(site.SERVER_HEADER_NAME, site.SERVER_HEADER_VALUE);
// Parse the incoming user token from the client header
// Attach parsed token data to request for downstream use
request.tokenContext = site.parseClientToken(request.headers[site.CLIENT_HEADER_NAME]);
});
// -----------------------------------------------------------------------------
// Routes
// -----------------------------------------------------------------------------
app.get("/", async (request, reply) => {
// Access parsed `tokenContext` for this request
const tokenContext = request.tokenContext;
// Render HTML template using `tokenContext` to adjust feature display
const state = (value: boolean) => (value && '<b style="background: #b0b0b067">YES</b>') || "NO";
const template = `
<html>
<body>
<h1>Hello</h1>
<h3>Contents of "tokenContext" variable for this request:</h3>
<pre style="display: inline-block; border: 1px solid #5b5b5ba4; padding: 0.5rem; background: #b0b0b067">tokenContext = ${JSON.stringify(tokenContext, null, 2)}</pre>
<h3>Site Feature toggles to be used while rendering this page:</h3>
<ul>
<li>Hide Advertisements: ${state(tokenContext.HIDE_ADVERTISEMENTS)}</li>
<li>Hide Cookie Consent Dialog: ${state(tokenContext.HIDE_COOKIE_CONSENT_SCREEN)}</li>
<li>Hide Marketing Dialogs: ${state(tokenContext.HIDE_MARKETING_DIALOGS)}</li>
<li>Disable 3rd Party non-functional tracking: ${state(tokenContext.DISABLE_NON_FUNCTIONAL_TRACKING)}</li>
<li>Disable Content Paywalls: ${state(tokenContext.DISABLE_CONTENT_PAYWALL)}</li>
<li>Enable Free access to your Base Subscription plan: ${state(tokenContext.ENABLE_SUBSCRIPTION_ACCESS)}</li>
</ul>
</body>
</html>
`;
reply.type("text/html").send(template);
});
app.get("/json", async (request) => {
// Return JSON response with `tokenContext` for API usage
return {
message: "OK",
tokenContext: request.tokenContext,
};
});
// -----------------------------------------------------------------------------
// Start Fastify server
// -----------------------------------------------------------------------------
app.listen({ port: 3000 }, () => {
console.log(`Fastify server listening at port 3000
· HTML template example: http://localhost:3000
· Plain JSON endpoint example: http://localhost:3000/json`);
});