Skip to Content
Drash v3 is in beta. APIs may change.
DocsQuickstartCloudflare Workers

Cloudflare Workers

Workers provide URLPattern natively, so use the native entry point.

Install

npm install @drashland/drash npm install --save-dev wrangler

App

A Worker’s module entry point is a default export with a fetch method. Build the chain once at module scope — it is reused across requests in the same isolate.

import { Application, Resource, } from "@drashland/drash/modules/http.native.js"; class Home extends Resource { paths = ["/"]; GET(request) { return new Response("Oh so easy"); } } const app = Application .builder() .resources(Home) .build(); const handleRequest = (request, _bindings) => { return app .handle(request) .catch(() => { return new Response("Sorry, but we hit an error!", { status: 500, statusText: "Internal Server Error", }); }); }; export default { fetch: handleRequest };

Run

npx wrangler dev app.js

Then visit the address wrangler prints (http://localhost:8787 by default).

Notes

Every named export of a Worker entry module must be a function, a class, or an ExportedHandler. workerd treats named exports as potential entrypoints and validates them at startup, so a stray constant like:

export const hostname = "localhost";

aborts the Worker before it serves anything, with an error naming that export:

Uncaught TypeError: Incorrect type for map entry 'hostname': the provided value is not of type 'function or ExportedHandler'.

Keep configuration values as module-local consts, not exports.

A runnable version lives in examples/cloudflare-workers-js.

Last updated on