Welcome to Danniverse ݁ Λ–πŸͺ.π–₯”Λš

Implement Upvote feature using Cloudflare Workers + KV

I followed https://github.com/rokcso/post-upvote-api and added the Upvote feature to my site. Now visitors can upvote the blogs and the upvote count and status is displayed. What I did differently with the tutorial was that I went through the steps through C3 and Wrangler instead of Cloudflare dashboard.

Create and Deploy Worker

  1. Create a new Worker project
npm create cloudflare@latest -- post-upvote

For setup,

dannishi@DanniIsWorking cloudflare % npm create cloudflare@latest -- post-upvote

> npx
> "create-cloudflare" post-upvote


────────────────────────────────────────────────────────────────────────────────
πŸ‘‹ Welcome to create-cloudflare v2.52.3!
🧑 Let's get started.
πŸ“Š Cloudflare collects telemetry about your usage of Create-Cloudflare.

Learn more at: https://github.com/cloudflare/workers-sdk/blob/main/packages/create-cloudflare/telemetry.md
────────────────────────────────────────────────────────────────────────────────

β•­ Create an application with Cloudflare Step 1 of 3
β”‚
β”œ In which directory do you want to create your application?
β”‚ dir ./post-upvote
β”‚
β”œ What would you like to start with?
β”‚ category Hello World example
β”‚
β”œ Which template would you like to use?
β”‚ type Worker only
β”‚
β”œ Which language do you want to use?
β”‚ lang JavaScript
β”‚
β”œ Copying template files
β”‚ files copied to project directory
β”‚
β”œ Updating name in `package.json`
β”‚ updated `package.json`
β”‚
β”œ Installing dependencies
β”‚ installed via `npm install`
β”‚
β•° Application created 

β•­ Configuring your application for Cloudflare Step 2 of 3
β”‚
β”œ Installing wrangler A command line tool for building Cloudflare Workers
β”‚ installed via `npm install wrangler --save-dev`
β”‚
β”œ Retrieving current workerd compatibility date
β”‚ compatibility date 2025-10-11
β”‚
β”œ Do you want to use git for version control?
β”‚ yes git
β”‚
β”œ Initializing git repo
β”‚ initialized git
β”‚
β”œ Committing new files
β”‚ git commit
β”‚
β•° Application configured 

β•­ Deploy with Cloudflare Step 3 of 3
β”‚
β”œ Do you want to deploy your application?
β”‚ no deploy via `npm run deploy`
β”‚
β•° Done 

────────────────────────────────────────────────────────────
πŸŽ‰  SUCCESS  Application created successfully!

πŸ’» Continue Developing
Change directories: cd post-upvote
Start dev server: npm run start
Deploy: npm run deploy

πŸ“– Explore Documentation
https://developers.cloudflare.com/workers

πŸ› Report an Issue
https://github.com/cloudflare/workers-sdk/issues/new/choose

πŸ’¬ Join our Community
https://discord.cloudflare.com
────────────────────────────────────────────────────────────
  1. Replace the source code Replace the code in src/index.js with the code in worker.js. Run the wrangler dev command in the project directory to start a local server and preview the Worker locally at http://localhost:8787/.
npx wrangler dev

You should be seeing

{"code":0,"msg":"Visit https://rokcso.com/p/post-upvote-api/ to see more."}
  1. Deploy the Worker
npx wrangler deploy

Preview your Worker at <YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev.

Create KV namespace and bind it with Worker

npx wrangler kv namespace create upvote-count

Once you run this command, Wrangler will prompt you a few questions and help create the binding to connect Worker with your KV namespace.

πŸŒ€ Creating namespace with title "upvote-count"
✨ Success!
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
{
  "kv_namespaces": [
    {
      "binding": "upvote_count",
      "id": "******"
    }
  ]
}
βœ” Would you like Wrangler to add it on your behalf? … yes
βœ” What binding name would you like to use? … UPVOTE_COUNT
βœ” For local dev, do you want to connect to the remote resource instead of a local resource? … no

Wrangler manage the bindings through wrangler.jsonc file.

"kv_namespaces": [
		{
			"binding": "UPVOTE_COUNT",
			"id": "******"
		}
	]

Create and bind another namespace ‘upvote-record’

npx wrangler kv namespace create upvote-record

Deploy the Worker again with KV namespace

npx wrangler deploy

You should be seeing something like

Your Worker has access to the following bindings:
Binding                                                   Resource          
env.UPVOTE_COUNT (******)       KV Namespace      
env.UPVOTE_RECORD (******)      KV Namespace      

Uploaded post-upvote (2.65 sec)
Deployed post-upvote triggers (1.01 sec)

Go to Cloudflare dashboard, you should be seeing

KV namespace and bindings are successfully created
KV namespace and bindings are successfully created

Update hugo.toml to enable upvote

[params]
    upvote = true
    upvoteURL = "The domain name of the Worker that was just deployed/" 

Ta-da! Please click the Upvote button below to help me testing πŸ˜‰

#Danniverse #Dev #Cloudflare