You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/en/core-concepts/endpoints.mdx
+15-19Lines changed: 15 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ Endpoints export a `get` function (optionally `async`) that receives a [context
17
17
```ts
18
18
// Example: src/pages/builtwith.json.ts
19
19
// Outputs: /builtwith.json
20
-
exportasyncfunctionget({params, request}) {
20
+
exportasyncfunctionGET({params, request}) {
21
21
return {
22
22
body: JSON.stringify({
23
23
name: 'Astro',
@@ -30,7 +30,7 @@ export async function get({params, request}) {
30
30
The return object can also have an `encoding` property. It can be any valid [`BufferEncoding`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/bdd02508ddb5eebcf701fdb8ffd6e84eabf47885/types/node/buffer.d.ts#L169) accepted by Node.js' `fs.writeFile` method. For example, to produce a binary png image:
@@ -109,7 +109,7 @@ Server endpoints can access `params` without exporting `getStaticPaths`, and the
109
109
```jstitle="src/pages/[id].json.js"
110
110
import { getProduct } from'../db';
111
111
112
-
exportasyncfunctionget({ params }) {
112
+
exportasyncfunctionGET({ params }) {
113
113
const id =params.id;
114
114
const product =awaitgetProduct(id);
115
115
@@ -134,7 +134,7 @@ This will respond to any request that matches the dynamic route. For example, if
134
134
In SSR mode, certain providers require the `Content-Type` header to return an image. In this case, use a `Response` object to specify a `headers` property. For example, to produce a binary `.png` image:
In addition to the `get` function, you can export a function with the name of any [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). When a request comes in, Astro will check the method and call the corresponding function.
147
+
In addition to the `GET` function, you can export a function with the name of any [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). When a request comes in, Astro will check the method and call the corresponding function.
148
148
149
-
You can also export an `all` function to match any method that doesn't have a corresponding exported function. If there is a request with no matching method, it will redirect to your site's [404 page](/en/core-concepts/astro-pages/#custom-404-error-page).
150
-
151
-
:::note
152
-
Since `delete` is a reserved word in JavaScript, export a `del` function to match the delete method.
153
-
:::
149
+
You can also export an `ALL` function to match any method that doesn't have a corresponding exported function. If there is a request with no matching method, it will redirect to your site's [404 page](/en/core-concepts/astro-pages/#custom-404-error-page).
In SSR mode, the `request` property returns a fully usable [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object that refers to the current request. This allows you to accept data and check headers:
193
189
194
190
```tstitle="src/pages/test-post.json.ts"
195
-
exportconstpost:APIRoute=async ({ request }) => {
191
+
exportconstPOST:APIRoute=async ({ request }) => {
196
192
if (request.headers.get("Content-Type") ==="application/json") {
197
193
const body =awaitrequest.json();
198
194
const name =body.name;
@@ -212,7 +208,7 @@ The endpoint context exports a `redirect()` utility similar to `Astro.redirect`:
0 commit comments