Cloud//Cloudflare//Wrangler
>> 26.03.10 - building a CRM at 2am. wrangler runs all of cloudflare on localhost. frontend, backend, db. i mass years setting up express servers like a peasant. wrangler.toml + npx wrangler dev and you're done.
26 · mar 10(day zero) building a CRM at 2am. wrangler runs all of cloudflare on localhost. frontend, backend, db. i mass years setting up express servers like a peasant. wrangler.toml + npx wrangler dev and you're done.
Cloudflare's CLI tool. The bridge between your machine and Cloudflare: the same code that runs on localhost:8788 is exactly what will run in production. Nothing to change.
Manages Workers, Pages, D1, R2, KV, and all other Cloudflare services from the terminal
Configuration via wrangler.toml. Authenticates via wrangler login (OAuth flow).
Runs via npx (npx wrangler dev) without global installation
All backend code is TypeScript: CRUD routes, SQL queries to D1, automations (e.g. creating a contact triggers creating its associated story -- all in the same handler)
Remote (production):
wrangler pages deploy dist --> uploads dist/ contents to Pages. Cloudflare distributes it to the edge.
Each request spins up a Worker: it's born, executes the backend (Hono), responds, and dies (serverless). No process running 24/7.
wrangler d1 migrations apply --remote --> applies migrations to the production D1
Local (development):
wrangler pages dev dist > simulates Cloudflare on your machine at localhost:8788. Acts as a local server like Node.js/Express would, but running code in an environment that mimics Workers same result as production.
Serves frontend (from dist/) + backend (functions/) + local D1, all on one port. No HMR for frontend -- serves pre-compiled files.
Wrangler has HMR for functions (API): change a handler, it reloads. But frontend needs manual rebuild.
Two-terminal mode (recommended for development): Terminal 1: Vite (npm run dev, port 5173) > frontend with HMR. Terminal 2: Wrangler (wrangler pages dev dist, port 8788) > API + D1 only. Vite proxies /api/* to Wrangler. Open localhost:5173, not 8788.
In two-terminal mode, Wrangler doesn't actually need dist/ (it's not serving frontend), but wrangler pages dev requires a directory argument. If dist/ doesn't exist, an empty mkdir dist works fine.