Guide

Server plugins


Plugins allow creating reusable middleware to intercept request lifecycle.

Example

import { serve, type ServerPlugin } from "srvx";

const logRequests: ServerPlugin = (server) => {
  console.log(`Logger plugin enabled for ${server.runtime}`);
  return {
    name: "log",
    fetch: (req, next) => {
      console.log(`[request] [${req.method}] ${req.url}`);
      return next();
    },
  };
};

const xPoweredBy: ServerPlugin = {
  fetch: (req, next) => {
    const res = await next();
    res.headers.set("X-Powered-By", "srvx");
    return res;
  },
};

serve({
  plugins: [logRequests, xPoweredBy],
  fetch(request) {
    return new Response(`👋 Hello there.`);
  },
});