|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} HTTP Methods`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn about how to add new API routes for each HTTP method. |
|
8
|
|
|
9
|
## HTTP Method Handler |
|
10
|
|
|
11
|
An API route is created for every HTTP method you export a handler function for in a `route.ts` or `route.js` file. |
|
12
|
|
|
13
|
Allowed HTTP methods are: `GET`, `POST`, `DELETE`, `PUT`, `PATCH`, `OPTIONS`, and `HEAD`. |
|
14
|
|
|
15
|
For example, create the file `src/api/hello-world/route.ts` with the following content: |
|
16
|
|
|
17
|
```ts title="src/api/hello-world/route.ts" |
|
18
|
import type { |
|
19
|
MedusaRequest, |
|
20
|
MedusaResponse, |
|
21
|
} from "@medusajs/framework/http" |
|
22
|
|
|
23
|
export const GET = async ( |
|
24
|
req: MedusaRequest, |
|
25
|
res: MedusaResponse |
|
26
|
) => { |
|
27
|
res.json({ |
|
28
|
message: "[GET] Hello world!", |
|
29
|
}) |
|
30
|
} |
|
31
|
|
|
32
|
export const POST = async ( |
|
33
|
req: MedusaRequest, |
|
34
|
res: MedusaResponse |
|
35
|
) => { |
|
36
|
res.json({ |
|
37
|
message: "[POST] Hello world!", |
|
38
|
}) |
|
39
|
} |
|
40
|
``` |
|
41
|
|
|
42
|
This file adds two API Routes: |
|
43
|
|
|
44
|
- A `GET` API route at `http://localhost:9000/hello-world`. |
|
45
|
- A `POST` API route at `http://localhost:9000/hello-world`. |