This folder contains the server-side code for the MolstarViewer application.
backend/
├── index.ts # Main HTTP entry point for Val Town
└── README.md # This file
(Serves files from frontend/ folder at project root)
index.ts is the main HTTP handler that Val Town calls when your val receives requests.
It uses Hono (a lightweight web framework) to:
/frontend/* directory/frontend/index.html for all other routes (enabling client-side routing)import { Hono } from "jsr:@hono/hono@4";
import { serveFile } from "https://esm.town/v/std/utils@85-main/index.ts";
const app = new Hono();
// Error handling
app.onError((err, c) => {
throw err; // Shows full stack traces
});
// Serve frontend files
app.get("/frontend/*", (c) => serveFile(c.req.path, import.meta.url));
// SPA: serve index.html for all other routes
app.get("*", (c) => serveFile("/frontend/index.html", import.meta.url));
export default app.fetch; // Val Town entry point
/frontend/index.html, letting React handle routing client-sidebackend/database/ if needed)index.ts exports app.fetch as default, which Val Town recognizes as an HTTP handlerfrontend/ folder// Add before the catch-all route
app.get("/api/structures", async (c) => {
return c.json({ structures: [...] });
});
Create backend/database/ folder with:
migrations.ts - Schema definitionsqueries.ts - Database query functionsCreate backend/routes/ folder for modular route handlers.
backend/index.ts as an HTTP handler because it exports a default fetch handlerimport.meta.url gives context about the current val's locationserveFile utility handles reading files from the project and setting correct content types/frontend/* not ../frontend/*)