首页 > 其他分享 >使用wrangler创建hono和D1的Worker

使用wrangler创建hono和D1的Worker

时间:2024-07-17 18:18:25浏览次数:12  
标签:wrangler app Worker hono tutorial d1

以下内容参考了
https://developers.cloudflare.com/d1/get-started/
https://developers.cloudflare.com/d1/examples/d1-and-hono/

1:登录

npx wrangler login

2:创建Worker

npm create cloudflare@latest d1-tutorial

Choose "Hello World" Worker for the type of application.
Select yes to using TypeScript.
Select no to deploying.

3:创建数据库

npx wrangler d1 create prod-d1-tutorial

4:绑定数据库

cd d1-tutorial
nano wrangler.toml

在末尾添加

[[d1_databases]]
binding = "DB"
database_name = "prod-d1-tutorial"
database_id = "<unique-ID-for-your-database>"

5:导入测试数据
将下面代码保存成schema.sql

DROP TABLE IF EXISTS Customers;
CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY, CompanyName TEXT, ContactName TEXT);
INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (1, 'Alfreds Futterkiste', 'Maria Anders'), (4, 'Around the Horn', 'Thomas Hardy'), (11, 'Bs Beverages', 'Victoria Ashworth'), (13, 'Bs Beverages', 'Random Name');

然后用下面代码导入

npx wrangler d1 execute prod-d1-tutorial --local --file=./schema.sql

6:安装依赖库

npm i hono

7:修改代码

echo ''> src/index.ts
nano src/index.ts

贴入下面代码

import { Hono } from "hono";

// This ensures c.env.DB is correctly typed
type Bindings = {
  DB: D1Database;
};

const app = new Hono<{ Bindings: Bindings }>();

// Accessing D1 is via the c.env.YOUR_BINDING property
app.get("/api/beverages/:id", async (c) => {
  const customerID = c.req.param("id");
  try {
    let { results } = await c.env.DB.prepare(
      "SELECT * FROM Customers WHERE CustomerID = ?",
    )
      .bind(customerID)
      .all();
    return c.json(results);
  } catch (e) {
    return c.json({ err: e.message }, 500);
  }
});

// Export our Hono app: Hono automatically exports a
// Workers 'fetch' handler for you
export default app;

8:本地测试代码

npm run start

浏览器打开下面网址

localhost:8787/api/beverages/4

9:远程测试代码

npm run deploy

浏览器打开下面网址

https://d1-tutorial.test.workers.dev/api/beverages/4

10:发布代码

npx wrangler publish

标签:wrangler,app,Worker,hono,tutorial,d1
From: https://www.cnblogs.com/xiangxisheng/p/18308064

相关文章

  • js worker
    index.js<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</tit......
  • mongoDB 报错 MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 : 一个可行的
    今天启用mongoshell时发现报错如下:尝试数据指令mongod启动服务器也没有作用,上网查询解决方案后发现是没有在service里面启动mongodb服务,启动该服务后再键入mongosh命令即可正常运行mongoshell。具体操作如下:STEP1:win+R➡️输入services.msc➡️确定 STEP2:找到MongoD......
  • C#Worker Service征服Linux:从零到部署英雄的跨界之旅
    ......
  • Mybatis Plus 3.X版本的insert填充自增id的IdType.ID_WORKER策略源码分析
    总结/朱季谦某天同事突然问我,你知道MybatisPlus的insert方法,插入数据后自增id是如何自增的吗?我愣了一下,脑海里只想到,当在POJO类的id设置一个自增策略后,例如@TableId(value="id",type=IdType.ID_WORKER)的注解策略时,就能实现在每次数据插入数据库时,实现id的自增,例如以下形式......
  • thinkphp6 使用Workerman测试
    1.首先通过composer安装 think-worker、think-viewcomposerrequiretopthink/think-workercomposerrequiretopthink/think-view安装成功后在项目的config目录会新增3个配置文件 2.修改woker.php中监听的端口return[//扩展自身需要的配置'host'......
  • LeetCode 857. Minimum Cost to Hire K Workers
    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-hire-k-workers/description/题目:Thereare n workers.Youaregiventwointegerarrays quality and wage where quality[i] isthequalityofthe ith workerand wage[i] istheminimumwagee......
  • worker.cpp
    include<worker.h>employee::employee(intid,stringname,intpost){this->m_id=id;this->m_name=name;this->m_job=post;}voidemployee::m_show(){cout<<"职工编号:"<<this->m_id<<"\t职工姓名:"......
  • worker.h
    pragmaonceincludeusingnamespacestd;includeclassworker{public:virtualvoidm_show()=0;virtualstringm_post()=0;intm_id;//职工编号stringm_name;//职工姓名intm_job;//职工岗位};classemployee:publicworker{public:employee(intid,st......
  • 基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务
    本文主要介绍了如何基于CloudflareWorkers和cloudflare-docker-proxy搭建dockerhub、gcr、quay等镜像加速服务。最近,受限于各种情况,部分主流镜像站都关了,为了能够正常使用,建议自己搭建一个加速器。写文之前,也已经部署好了一个,可以直接使用,具体使用方法跳转https://docke......
  • Chrome扩展开发使 Service Worker 保持活跃状态
    ServiceWorker设计为独立于单个页面的后台线程,它通常在没有控制任何页面(clients)时会被浏览器停止或处于等待状态。但是,如果你希望即使在插件页面关闭后,ServiceWorker仍然保持活跃,可以尝试以下方法:使用多个页面:保持至少一个页面(如后台管理页面)打开,该页面由ServiceWorker控......