http-endpoints
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHTTP Endpoints
HTTP 端点
HTTP vals () export a request handler and run on every incoming HTTP request. Each HTTP file is assigned a live URL — never construct it yourself; read from or responses, or call .
fileType: "http"links.endpointlist_filescreate_filefetch_val_endpointThat URL is open to anyone unless the val's app access () is , in which case unauthenticated callers get a to a login page instead of your response — see the skill.
httpPrivacyrestricted302restricted-accessHTTP val()会导出一个请求处理器,针对每个传入的 HTTP 请求运行。每个 HTTP 文件都会被分配一个实时 URL——请勿自行拼接构造;请从 或 的返回结果中读取 ,或调用 获取。
fileType: "http"list_filescreate_filelinks.endpointfetch_val_endpoint该 URL 默认对所有人开放,除非 val 的应用访问权限()设为 ——这种情况下,未认证的调用者会收到 重定向到登录页,而非你的响应内容,详见 技能。
httpPrivacyrestricted302restricted-accessBasic handler
基础处理器
ts
// Learn more: https://docs.val.town/vals/http/
export default async function (req: Request): Promise<Response> {
return Response.json({ ok: true });
}The file must have an — for the handler.
exportexport defaultts
// Learn more: https://docs.val.town/vals/http/
export default async function (req: Request): Promise<Response> {
return Response.json({ ok: true });
}文件必须包含 语句——处理器需使用 导出。
exportexport defaultHono
Hono
When using Hono, export (not ):
app.fetchappts
import { Hono } from "npm:hono";
import { parseVal, serveImmutableFile } from "https://esm.town/v/std/utils/index.ts";
const app = new Hono();
app.get("/", (c) => c.text("hello"));
// Immutable asset caching (see the client-side-js skill): serves the
// current-version URLs your HTML shell stamps with immutableFileUrl()
app.get("/__immutable/*", (c) => serveImmutableFile(c.req.path));
// View source redirect
app.get("/source", (c) => c.redirect(parseVal().links.self.val));
// Always add this for full stack traces on errors:
app.onError((err) => Promise.reject(err));
export default app.fetch;Hono's does not work on Val Town. Use / from for static files. For the full API (, , , , , , , …), fetch .
serveStaticserveFilestaticHTTPServerstd/utilsstd/utilsreadFileserveFilestaticHTTPServerlistFileslistFilesByPathhttpEndpointparseValhttps://utilities.val.run/docs.md使用 Hono 时,请导出 (而非 本身):
app.fetchappts
import { Hono } from "npm:hono";
import { parseVal, serveImmutableFile } from "https://esm.town/v/std/utils/index.ts";
const app = new Hono();
app.get("/", (c) => c.text("hello"));
// Immutable asset caching (see the client-side-js skill): serves the
// current-version URLs your HTML shell stamps with immutableFileUrl()
app.get("/__immutable/*", (c) => serveImmutableFile(c.req.path));
// View source redirect
app.get("/source", (c) => c.redirect(parseVal().links.self.val));
// Always add this for full stack traces on errors:
app.onError((err) => Promise.reject(err));
export default app.fetch;Hono 的 无法在 Val Town 上运行。静态文件请使用 中的 / 。如需完整的 API 文档(、、、、、、 等),请访问 。
serveStaticstd/utilsserveFilestaticHTTPServerstd/utilsreadFileserveFilestaticHTTPServerlistFileslistFilesByPathhttpEndpointparseValhttps://utilities.val.run/docs.mdCORS
CORS
Val Town adds permissive CORS headers by default (), so in 99% of cases, you should never need to do anything with CORS. Using Hono's middleware is almost always unnecessary.
Access-Control-Allow-Origin: *corsIf you set any CORS header yourself, Val Town stops adding all default headers — so either handle CORS completely yourself or don't touch it at all.
Val Town 默认会添加宽松的 CORS 头(),因此 99% 的场景下你无需处理 CORS 相关配置。使用 Hono 的 中间件几乎是不必要的。
Access-Control-Allow-Origin: *cors如果你自行设置了任何 CORS 头,Val Town 就会停止添加所有默认头——因此要么完全自行处理 CORS,要么完全不要修改相关配置。
Redirects
重定向
Response.redirectts
return new Response(null, { status: 302, headers: { Location: "/path" } });
// or, with Hono:
return c.redirect("/path");Response.redirectts
return new Response(null, { status: 302, headers: { Location: "/path" } });
// or, with Hono:
return c.redirect("/path");What's not available
不支持的功能
- WebSockets: Val Town does not accept incoming WebSocket connections. Use polling, long polling, or server-sent events instead.
- Filesystem access: see the platform constraints. For persistent state, use or
std/sqlite.std/blob
- WebSockets:Val Town 不接受传入的 WebSocket 连接。请使用轮询、长轮询或服务器发送事件(SSE)替代。
- 文件系统访问:详见平台约束。如需持久化状态,请使用 或
std/sqlite。std/blob
Surfacing client-side errors
上报客户端错误
For HTML responses, add this script tag to send browser errors back to val logs (visible via ):
get_logshtml
<script src="https://esm.town/v/std/catch"></script>对于 HTML 响应,添加以下 script 标签可将浏览器错误回传到 val 日志(可通过 查看):
get_logshtml
<script src="https://esm.town/v/std/catch"></script>Verifying changes
验证变更
After editing an HTTP val, fetch it to confirm it returns the expected HTTP response. Do not report a change as done without this step.
编辑 HTTP val 后,请发起请求确认其返回符合预期的 HTTP 响应。未完成此步骤请勿宣称变更已完成。