An Express + TypeScript API that forwards code-generation prompts to the OpenRouter API, enforces a JavaScript-only context, and returns HTML-highlighted snippets with quick reference links.
- Copy
.env.exampleto.envand provide your OpenRouter credentials. - Install dependencies (requires Node.js 18+ for the built-in
fetchAPI):
npm installnpm run dev— start the API in watch mode viats-node.npm run build— compile TypeScript todist/.npm start— run the compiled server fromdist/.npm test— run the Vitest suite (uses JSDOM for HTML validation).
POST /api/generate
Request body:
{
"question": "How do I animate a div on click?"
}Behavior:
- Wraps the question with a system prompt that demands only JavaScript code (no Markdown).
- Sends the request to OpenRouter using the configured model.
- Highlights the returned JavaScript with
highlight.jsand converts selected function names into links to MDN.
Response body:
{
"html": "<pre><code class=\"hljs language-javascript\">…</code></pre>"
}PORT— API port (default:3000).OPENROUTER_API_KEY— required OpenRouter token.OPENROUTER_MODEL— model slug (default:openai/gpt-4o-mini).OPENROUTER_SITE_URL— value forwarded to OpenRouterHTTP-Refererheader.OPENROUTER_APP_TITLE— value forwarded to OpenRouterX-Titleheader.
- The service expects the OpenRouter API key to be present at runtime. Without it, requests will fail.
- Function references such as
fetch,console.log,document.querySelector, andaddEventListenerare automatically linked to MDN in the generated HTML.