|
| 1 | +--- |
| 2 | +pcx-content-type: tutorial |
| 3 | +title: Writing your first Function |
| 4 | +weight: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +# Writing your first Function |
| 8 | + |
| 9 | +When writing request handlers within your Pages application, each `/functions` file must `export` a function to handle the incoming request. Each function will receive a singular `context` object, which contains all the information for the request: |
| 10 | + |
| 11 | +```js |
| 12 | +export async function onRequest(context) { |
| 13 | + // Contents of context object |
| 14 | + const { |
| 15 | + request, // same as existing Worker API |
| 16 | + env, // same as existing Worker API |
| 17 | + params, // if filename includes [id] or [[path]] |
| 18 | + waitUntil, // same as ctx.waitUntil in existing Worker API |
| 19 | + next, // used for middleware or to fetch assets |
| 20 | + data, // arbitrary space for passing data between middlewares |
| 21 | + } = context; |
| 22 | + |
| 23 | + return new Response("Hello, world!"); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +When migrating from a [Module Worker](/workers/runtime-apis/fetch-event/#syntax-module-worker), this signature combines the traditional `fetch` handler's arguments into a single object along with additional, Pages-specific keys. |
| 28 | + |
| 29 | +In the previous example, an `onRequest` function was exported. This is a generic name because it generically handles all HTTP requests. However, to react to specific HTTP request methods, you may use the method name as a suffix to the exported function. For example, a handler that should only receive `GET` requests should be named `onRequestGet`. The following other handlers are supported: |
| 30 | + |
| 31 | +- `onRequestPost` |
| 32 | +- `onRequestPut` |
| 33 | +- `onRequestPatch` |
| 34 | +- `onRequestDelete` |
| 35 | +- `onRequestHead` |
| 36 | +- `onRequestOptions` |
| 37 | + |
| 38 | +These are the requests you export to write your first function. For example, you can write a function to output `"Hello World"` when it hits a `/functions/hello-world.js` file: |
| 39 | + |
| 40 | +```js |
| 41 | +--- |
| 42 | +filename: functions/hello-world.js |
| 43 | +--- |
| 44 | +// Reacts to POST /hello-world |
| 45 | +export async function onRequestPost(request) { |
| 46 | + // ... |
| 47 | + return new Response(`Hello world`); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Another helpful example for handling single path segments can be querying an API for data, for example, [Rick and Morty API](https://rickandmortyapi.com/documentation/#rest) for information on the show characters. You can write a function to show each character on request using the ID to identify them: |
| 52 | + |
| 53 | +```js |
| 54 | +--- |
| 55 | +filename:function/character/[id].js |
| 56 | +--- |
| 57 | +export async function onRequestGet({ params }) { |
| 58 | + const res = await fetch(`https://rickandmortyapi.com/api/character/${params.id}`); |
| 59 | + const data = await res.json(); |
| 60 | + const info = JSON.stringify(data, null, 2); |
| 61 | + return new Response(info); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +The above will return each character at `/character/{id}` ID being associated with the character. |
| 66 | + |
| 67 | +### Handling multiple requests in a single function |
| 68 | + |
| 69 | +You can define multiple HTTP handlers in a single file by defining multiple exports within the same file. For example, this file will handle `POST` and `PUT` requests with the same handler code: |
| 70 | + |
| 71 | +```js |
| 72 | +export async function onRequestPost(context) { |
| 73 | + // ... |
| 74 | +} |
| 75 | + |
| 76 | +export const onRequestPut = onRequestPost; |
| 77 | +``` |
| 78 | + |
| 79 | +Additionally, an exported handler may be an array of function handlers. This allows you to easily compose Functions as a group, which may include a mix of shared and/or one-off behaviors: |
| 80 | + |
| 81 | +```js |
| 82 | +import { extraLogging } from "middlewares.ts"; |
| 83 | + |
| 84 | +export const onRequest = [ |
| 85 | + extraLogging, |
| 86 | + |
| 87 | + async ({ request }) => { |
| 88 | + // ... |
| 89 | + }, |
| 90 | +]; |
| 91 | +``` |
0 commit comments