---
title: "Generic Middleware — Markdown Mirrors"
description: "Cloudflare Worker, Nginx, or Express middleware to expose .md mirrors."
type: "page"
canonical: "https://audigeo.ai/docs/integrations/markdown-mirrors/generic-middleware"
---

# Generic Middleware

If your stack isn't covered, intercept `*.md` requests in front of your origin.

## Cloudflare Worker

```js
addEventListener("fetch", (event) => event.respondWith(handle(event.request)));

async function handle(req) {
  const url = new URL(req.url);
  if (!url.pathname.endsWith(".md")) return fetch(req);

  const htmlUrl = new URL(req.url);
  htmlUrl.pathname = htmlUrl.pathname.replace(/\.md$/, "");
  const html = await fetch(htmlUrl).then((r) => r.text());

  // naive HTML→MD; use a real lib in prod (e.g., turndown bundled into the worker)
  const md = html
    .replace(/<script[\s\S]*?<\/script>/g, "")
    .replace(/<style[\s\S]*?<\/style>/g, "")
    .replace(/<[^>]+>/g, "");

  return new Response(md, {
    headers: {
      "Content-Type": "text/markdown; charset=utf-8",
      "Cache-Control": "public, max-age=3600",
      "X-Robots-Tag": "index, follow",
    },
  });
}
```

## Nginx + sub-request

```nginx
location ~* \.md$ {
    rewrite ^(.+)\.md$ $1 break;
    proxy_pass http://origin;
    proxy_set_header Accept "text/markdown";
    add_header Content-Type "text/markdown; charset=utf-8";
    add_header Cache-Control "public, max-age=3600";
    add_header X-Robots-Tag "index, follow";
}
```

(Your origin must do the actual conversion when `Accept: text/markdown` is sent — pair this with a library on the backend.)

## Express middleware

```js
import express from "express";
import { JSDOM } from "jsdom";
import TurndownService from "turndown";

const td = new TurndownService();
const app = express();

app.get(/\.md$/, async (req, res) => {
  const htmlPath = req.path.replace(/\.md$/, "");
  const html = await fetch(`http://localhost:3000${htmlPath}`).then((r) => r.text());
  const dom = new JSDOM(html);
  const main = dom.window.document.querySelector("main") ?? dom.window.document.body;
  res.set({
    "Content-Type": "text/markdown; charset=utf-8",
    "Cache-Control": "public, max-age=3600",
    "X-Robots-Tag": "index, follow",
  });
  res.send(td.turndown(main.outerHTML));
});
```

## Verify

Run an AudiGEO audit. Target: `markdown_mirror` ≥ 7.
