从服务器调用端点
端点可用于提供多种数据。此秘诀从页面的组件脚本调用服务器端点来显示问候语,而不需要额外的获取请求。
¥Endpoints can be used to serve many kinds of data. This recipe calls a server endpoint from a page’s component script to display a greeting, without requiring an additional fetch request.
¥Prerequisites
- 启用了 SSR(输出:‘server’)的项目
¥Recipe
-
Create an endpoint in a new file
src/pages/api/hello.ts
that returns some data: -
On any Astro page, import the
GET()
method from the endpoint. Call it with theAstro
global to provide the request context, and use the response on the page: