This is a React project built for val.town, a platform for building and deploying serverless functions.
main.tsx - Main React component with server-side renderingdeno.json - Deno configuration with TypeScript and val.town types.vt/ - Val.town CLI configuration and state.vtignore - Files to ignore when deploying to val.townhttps://esm.sh/react as JSX import sourcereact-dom/server for renderToString# Deploy to val.town vt deploy # Run locally vt dev # Check val status vt status # Pull latest from val.town vt pull # Push changes to val.town vt push
# Lint with Deno deno lint # Type check with Deno deno check main.tsx # Format code deno fmt
364f6226-8700-11f0-9096-0224a6c84d84364fb17c-8700-11f0-9096-0224a6c84d84/** @jsxImportSource https://esm.sh/react */ at
the top of React filesrenderToString for HTML responsesRequest and return
Responsenpm: prefix for npm packages or ESM URLs/** @jsxImportSource https://esm.sh/react */
import { renderToString } from "npm:react-dom/server";
export const myHandler = (request: Request) =>
new Response(renderToString(<div>Hello World</div>), {
headers: {
"Content-Type": "text/html",
},
});
Val.town provides SQLite database access with every account:
import { sqlite } from "https://esm.sh/@valtown/sdk";
// Example SQLite usage
const db = sqlite("mydb");
await db.execute(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)",
);
await db.execute("INSERT INTO users (name) VALUES (?)", ["John"]);
const users = await db.execute("SELECT * FROM users");
SQLite Limits:
For more advanced database needs, integrate with Neon PostgreSQL:
Setup:
Usage:
import { Client } from "https://deno.land/x/postgres/mod.ts";
const client = new Client(Deno.env.get("DATABASE_URL"));
await client.connect();
// Example query
const result = await client
.queryObject`SELECT * FROM users WHERE id = ${userId}`;
await client.end();
PostgreSQL Best Practices:
"lock": false) for flexibility"node_modules_dir": false)