Skip to main content

Cloudflare Workers Deployment

The Sistema de Gestión de Propiedades API is built to run on Cloudflare Workers, a serverless platform that runs your code on Cloudflare’s global edge network.

Why Cloudflare Workers?

Global Edge Network

Deploy to 300+ data centers worldwide for low latency

Zero Cold Starts

V8 isolates start in milliseconds, not seconds

Automatic Scaling

Handle traffic spikes without configuration

Built-in D1 Database

SQLite database at the edge with global replication

Prerequisites

Before deploying, you need:
1

Cloudflare account

Create a free account at cloudflare.com
2

Node.js installed

Install Node.js 18+ from nodejs.org
3

Wrangler CLI

Install Cloudflare’s CLI tool:
npm install -g wrangler
4

Authenticate Wrangler

Log in to your Cloudflare account:
wrangler login

Project Structure

The backend project structure:
Backend/
├── src/
│   ├── controllers/
│   │   └── propiedades.controller.ts
│   ├── db/
│   │   └── schema.ts
│   ├── middlewares/
│   │   └── auth.ts
│   ├── routes/
│   │   ├── auth.ts
│   │   └── propiedades.ts
│   └── index.ts
├── schema.sql
├── package.json
├── tsconfig.json
└── wrangler.toml (you'll create this)

Dependencies

The project uses these dependencies (from package.json):
{
  "dependencies": {
    "@hono/swagger-ui": "^0.5.3",
    "@hono/zod-openapi": "^1.2.1",
    "hono": "^4.11.9",
    "zod": "^4.3.6"
  },
  "devDependencies": {
    "wrangler": "^4.4.0"
  }
}

Configuration

Create wrangler.toml

Create a wrangler.toml file in your Backend directory:
name = "inmobiliaria-propiedades-api"
main = "src/index.ts"
compatibility_date = "2024-01-01"

# D1 Database binding
[[d1_databases]]
binding = "DB"
database_name = "propiedades-db"
database_id = "<your-database-id>"

# Environment variables (for production)
[vars]
ADMIN_USER = "<your-admin-username>"
ADMIN_PASS = "<your-admin-password>"
Never commit wrangler.toml with real credentials. Use Wrangler secrets for sensitive data.

TypeScript Configuration

The tsconfig.json is already configured:
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ES2020",
    "moduleResolution": "node",
    "lib": ["ES2020"],
    "types": ["@cloudflare/workers-types"]
  }
}

Development

Local Development Server

Run the development server locally:
npm run dev
This starts Wrangler in development mode:
  • Hot reload on file changes
  • Local D1 database
  • Available at http://localhost:8787
The dev server uses a local SQLite database. Your production D1 database won’t be affected.

Test Locally

# List properties
curl http://localhost:8787/api/propiedades

# Create a property (with auth)
curl -u admin:password \
  -X POST http://localhost:8787/api/propiedades \
  -H "Content-Type: application/json" \
  -d '{"ciudad": "Test", ...}'

# View Swagger UI
open http://localhost:8787/ui

Database Setup

Create D1 Database

1

Create the database

wrangler d1 create propiedades-db
This outputs your database ID. Copy it to wrangler.toml.
2

Run migrations

wrangler d1 execute propiedades-db --file=./schema.sql
This creates the propiedades table.
3

Verify the table

wrangler d1 execute propiedades-db --command="SELECT * FROM propiedades"
For detailed database setup, see the Database Setup guide.

Deployment

Set Environment Variables

Store sensitive credentials as secrets:
# Set admin username
wrangler secret put ADMIN_USER
# Enter: your-admin-username

# Set admin password
wrangler secret put ADMIN_PASS
# Enter: your-admin-password
Secrets are encrypted and never visible in your code or Wrangler config.

Deploy to Production

npm run deploy
Or manually:
wrangler deploy --minify
This:
  1. Bundles your TypeScript code
  2. Minifies for optimal performance
  3. Uploads to Cloudflare’s edge network
  4. Assigns you a *.workers.dev subdomain

Deployment Output

⚙️  Building...
 Built successfully
🚀 Deploying to Cloudflare...
 Deployed to https://inmobiliaria-propiedades-api.yourname.workers.dev

Custom Domain

To use a custom domain like idforideas-1.jamrdev.com.ar:
1

Add domain to Cloudflare

Add your domain to Cloudflare and update nameservers.
2

Add route in wrangler.toml

routes = [
  { pattern = "idforideas-1.jamrdev.com.ar", zone_name = "jamrdev.com.ar" }
]
3

Deploy

wrangler deploy

Monitoring

View Logs

# Tail live logs
wrangler tail

# Filter by status code
wrangler tail --status=error

Check Metrics

View usage metrics in the Cloudflare Dashboard:
  • Request count
  • Error rate
  • CPU time
  • Bandwidth

Scripts

The package.json includes these scripts:
{
  "scripts": {
    "dev": "wrangler dev",
    "deploy": "wrangler deploy --minify",
    "cf-typegen": "wrangler types --env-interface CloudflareBindings"
  }
}
  • npm run dev - Start development server
  • npm run deploy - Deploy to production
  • npm run cf-typegen - Generate TypeScript types for Cloudflare bindings

Rollback

If a deployment has issues, rollback to a previous version:
1

List deployments

wrangler deployments list
2

Rollback

wrangler rollback --message "Rolling back due to issue"

Environment-Specific Configuration

For staging and production environments:
[env.staging]
name = "inmobiliaria-api-staging"

[[env.staging.d1_databases]]
binding = "DB"
database_name = "propiedades-staging"
database_id = "<staging-db-id>"

[env.production]
name = "inmobiliaria-api-production"

[[env.production.d1_databases]]
binding = "DB"
database_name = "propiedades-production"
database_id = "<production-db-id>"
Deploy to specific environment:
wrangler deploy --env staging
wrangler deploy --env production

Troubleshooting

Error: Cannot find module 'hono'Solution:
npm install
Error: D1_ERROR: no such table: propiedadesSolution:
wrangler d1 execute propiedades-db --file=./schema.sql
Cause: Secrets not setSolution:
wrangler secret put ADMIN_USER
wrangler secret put ADMIN_PASS
Cause: CORS is already enabled in the codeCheck: Verify app.use('*', cors()) is in src/index.ts:9-10

Best Practices

1

Use secrets for credentials

Never hardcode credentials in wrangler.toml
2

Enable minification

Always use --minify for production deployments
3

Monitor logs

Set up log tailing during initial deployment
4

Test locally first

Always test with wrangler dev before deploying
5

Use environment-specific configs

Separate staging and production databases

Next Steps

Database Setup

Detailed D1 database configuration

Environment Variables

Configure secrets and variables

API Reference

Explore the deployed API

Quickstart

Start using your deployed API

Build docs developers (and LLMs) love