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
- 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
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Replace the source code
Replace the code in
src/index.js
with the code in worker.js. Run thewrangler 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."}
- 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
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 π