Functions, bindings, and operations

Add one Pages Function

Create a file-based request handler and understand that Pages Functions execute on the Workers runtime.

8 minute lesson

~~~

A file under functions/ maps to a Pages route. The handler receives a context with the request, environment bindings, parameters, data, and a next function where appropriate.

Keep the first Function narrow, such as /api/health. Return correct statuses and content types. Remember that adding broad middleware can make every matching static asset invoke Worker code, changing both cost and failure surface.

Add a health Function, deploy with Wrangler or Git integration, and confirm a static asset still bypasses dynamic work.

Create one narrow function and keep static files outside its route:

export function onRequest() {
  return Response.json({ ok: true })
}

Save it as functions/api/health.js, deploy, and request /api/health. Then request a CSS file and verify it remains static. This check matters because an overly broad middleware route can turn every asset request into a metered function invocation.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →