
This folder contains all backend-only code in this project. It's broken up into:
index.ts
- this is the entrypoint for this whole projectdatabase/
- this contains the code for interfacing with the app's SQLite database table
This app uses Hono has the a API framework. You can think of Hono as a replacement for ExpressJS that works in serverless environments like Val Town or Cloudflare Workers. If you come from Python or Ruby, Hono is also a lot like Flask or Sinatra, respectively.
This HTML server is responsible for serving all static assets to the browser to render the app, including HTML, JavaScript (including all client-side React), CSS, and even the favicon SVG.
In a normal server environment, you would likely use a middleware like this one to serve static files. Some frameworks or deployment platforms automatically make any content inside a public/
folder public.
However in Val Town you need to handle this yourself, and it can be suprisingly difficult to read and serve files in a Val Town Project. This template uses helper functions from @stevekrouse/utils/serve-public, which handle reading project files in a way that will work across branches and forks, automatically transpiles typescript to javascript, and assigns content-types based on the file's extension.
The most complicated part of this backend API is serving index.html. In this app (like most apps) we serve it at the root, ie GET /
. This html file in turn imports the core index.tsx
file which mounts our client-side react component. (Browsers ignore file extensions, which is great, because all these files will be returned as transpiled JS with the JS content type by stevekrouse/utils/serve-public.)
What also makes serving index.html
complicated is that we bootstrap it with some data from the server, so that it gets dynamically injected JSON data without having to make another round-trip request to the server to get that data on the frontend. This is a common pattern for client-side rendered apps.
As of this writing Val Town only supports text files, which is why the favicon is an SVG and not an .ico or any other binary image format. If you need binary file storage, check out Blob Storage.
This app has two CRUD API routes: for reading and inserting into the messages table. They both speak JSON, which is standard. They import their functions from /backend/database/queries.ts
. This routes are called from the React app to refresh and update data.
Hono and other API frameworks have a habit of swallowing up Errors. We turn off this default behavior by re-throwing errors, because we think most of the time you'll want to see the full stack trace instead of merely "Internal Server Error". You can customize how you want errors to appear.