To install Volta, follow these steps:
-
Visit the Volta website and download the installer for your operating system.
-
Run the installer and follow the on-screen instructions to complete the installation.
-
Verify that Volta is installed by opening a new terminal window and running the following command:
volta --version
If the command displays the version number, Volta has been successfully installed.
-
You're now ready to use Volta for managing your project's Node.js version!
-
Pin Node Project version:
volta pin [email protected]
Skip this if your project already installed Wrangler
npm install @cloudflare/wrangler --save-dev
update Wrangler
npm install wrangler@latest
cd {SOURCE}/cloudflare-worker
npm create cloudflare@latest {project-name}
npm install eslint
npx eslint --init
Enpoint
: /test-kv
Method:
-
GET
:/test-kv?key=key-to-get-value
- Get the value of provided key
-
POST
:/test-kv
- Payload in form-data
{ "key1": "value1", "key2": "value2" }
- data in form will be stored in KV
-
DELETE
:/test-kv?key=key-to-delete
- Delete the key-value of provided key
Endpoint
: /redirect?url=redirect-url
Method:
ALL
:/redirect?url=https%3A%2F%2Fmy.mingne.dev
- should escape redirected url
Lear how to setup cron run schedule action in this sample worker
Create Rest API with Hono and integrate with D1 Database (a cloudflare serverless database)
- Setup Hono API App
app.route('/sample', router.sample); app.route('/auth', router.auth); app.route('/api', router.api); export default app
- Add Secret key environment in
{SOURCE}/worker-d1/.dev.vars
fileJWT_SECRET='dev-ne'
- Deploy key to Cloudflare worker
npx wrangler secret put JWT_SECRET
- Setup JWT Authention middleware for
/api/*
use JWT and get it from cookies
// * add JWT Authentification middleware app.use( '/api/*', async (c, next) => { const jwtMiddleware = jwt({ secret: c.env.JWT_SECRET, cookie: 'token', }); return jwtMiddleware(c, next) } )
-
Update D1 Database Configuration in wrangler.toml
-
Database Schema SQL: Schema
# migrate database cd {SOURCE}/cloudflare-worker/worker-d1 # yarn dev-d1-migrate # (for local environment) yarn d1-migrate