Skip to content

Commit 9ad8bda

Browse files
i18n(es): create recipes/call-endpoints.mdx (#14283)
Co-authored-by: paul valladares <85648028+dreyfus92@users.noreply.github.com>
1 parent 40e7865 commit 9ad8bda

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Llamar a los endpoints desde el servidor
3+
description: Aprende a realizar llamadas a endpoints desde el servidor en Astro.
4+
i18nReady: true
5+
type: recipe
6+
---
7+
8+
import { Steps } from '@astrojs/starlight/components';
9+
10+
Los endpoints se pueden usar para servir muchos tipos de datos. En esta receta se llama a un endpoint del servidor desde el script de un componente de la página para mostrar un saludo, sin necesidad de realizar una solicitud de recuperación adicional.
11+
12+
## Prerequisitos
13+
14+
- Un proyecto con [SSR](/es/guides/on-demand-rendering/) (output: 'server') habilitado.
15+
16+
## Receta
17+
18+
<Steps>
19+
1. Crea un endpoint en un nuevo archivo `src/pages/api/hello.ts` que devuelva algunos datos:
20+
21+
```ts title="src/pages/api/hello.ts"
22+
import type { APIRoute } from 'astro'
23+
24+
export const GET: APIRoute = () => {
25+
return new Response(
26+
JSON.stringify({
27+
greeting: 'Hola',
28+
}),
29+
)
30+
}
31+
```
32+
33+
2. En cualquier página Astro, importa el método `GET()` desde el endpoint. Llámalo con la [variable global `Astro`](/es/reference/api-reference/) para proporcionar el contexto de la solicitud, y usa la respuesta en la página:
34+
35+
```astro title="src/pages/index.astro"
36+
---
37+
import { GET } from './api/hello.ts';
38+
39+
let response = await GET(Astro)
40+
const data = await response.json()
41+
---
42+
43+
<h1>{data.greeting} mundo!</h1>
44+
```
45+
</Steps>

0 commit comments

Comments
 (0)