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:
Wrangler CLI
Install Cloudflare’s CLI tool:
Authenticate Wrangler
Log in to your Cloudflare account:
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:
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
Create the database
wrangler d1 create propiedades-db
This outputs your database ID. Copy it to wrangler.toml.
Run migrations
wrangler d1 execute propiedades-db --file=./schema.sql
This creates the propiedades table.
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
Or manually:
This:
Bundles your TypeScript code
Minifies for optimal performance
Uploads to Cloudflare’s edge network
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:
Add domain to Cloudflare
Add your domain to Cloudflare and update nameservers.
Add route in wrangler.toml
routes = [
{ pattern = "idforideas-1.jamrdev.com.ar" , zone_name = "jamrdev.com.ar" }
]
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:
List deployments
wrangler deployments list
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:
Error: D1_ERROR: no such table: propiedadesSolution: wrangler d1 execute propiedades-db --file=./schema.sql
Authentication fails after deployment
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
Use secrets for credentials
Never hardcode credentials in wrangler.toml
Enable minification
Always use --minify for production deployments
Monitor logs
Set up log tailing during initial deployment
Test locally first
Always test with wrangler dev before deploying
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