Cloudflare Workers API for portfolio content used by the Astro frontend:
- Frontend repo: https://github.com/ChhatreshKhatri/astro-portfolio
- Frontend service file:
src/services/api.ts - Frontend env var:
PUBLIC_API_URL
This API serves JSON data for:
/(default welcome payload)/home/about/projects
- Cloudflare Workers
- TypeScript
- Wrangler
- Vitest (Workers pool)
src/
index.ts # Worker fetch handler + routing + CORS
data.json # default payload
home.json # home page content
about.json # about page content
projects.json # projects page content
test/
index.spec.ts # test scaffold
wrangler.jsonc # Worker config
npm installnpm run devWrangler command equivalent:
npx wrangler devnpm run testThe Worker reads ALLOWED_ORIGINS from environment variables and only sets Access-Control-Allow-Origin when the request Origin matches one of the allowed entries.
Example (comma-separated):
ALLOWED_ORIGINS=https://www.chhatreshkhatri.com,http://localhost:4321For local dev, you can define this in a .dev.vars file used by Wrangler.
npm run deployWrangler command equivalent:
npx wrangler deployIf you change bindings in wrangler.jsonc, regenerate types:
npm run cf-typegenThe frontend repository is checked out locally at:
d:\Code\Projects\astro-portfolio
In that repo:
- API calls are centralized in
src/services/api.ts - Endpoints consumed are
/home,/about, and/projects - Set
PUBLIC_API_URLin frontend.envto this API base URL
Example frontend .env:
PUBLIC_API_URL=http://127.0.0.1:8787http://127.0.0.1:8787/home
http://127.0.0.1:8787/about
http://127.0.0.1:8787/projects
wrangler.jsonccurrently hasworkers_dev: false; production traffic should use configured routes/custom domain.- Current test file appears scaffolded from a default template and may not reflect current API responses.
- Cloudflare Workers docs: https://developers.cloudflare.com/workers/
- Cloudflare Workers limits: https://developers.cloudflare.com/workers/platform/limits/