如果你想将文本发布到世界上,并且您并不过分关心它的显示方式,那么 Pastebins 非常有用。无论你是 Pastes代码片段、业务草稿、长篇小说,还是重要的信息。 如果仅仅是想体验一下功能,可以去Pastebin官网。本次分享是给想自建一个Pastenbin的朋友

1. 准备条件

Cloudflare 账户,以及上面的一个域名

2. 安装部署

2.1 Fork Github pastebin-worker,并在本地clone下来

2.2 Installnodeandyarn and Install wrangler

1
2
3
4
 # 以下步骤在不同系统上有差异,以mac为例
1.brew install node
2.brew install yarn
3.npm i @cloudflare/wrangler -g

2.3 Create a KV namespace on Cloudflare workers dashboard, remember its ID.

KV的名字为’PB’, 创建好后把KV的ID填到wrangle.toml文件kv_namespaces的id后面

2.4 Clone the repository and enter the directory. Login to your Cloudflare account withwrangler login.

1
2
# 运行命令并授权
wrangler login

2.5 Modify entries inwrangler.toml. Its comments will tell you how.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 下面是我自己修改的配置
name = "pb" #worker name
compatibility_date = "2023-01-28"
compatibility_flags = [ "nodejs_compat" ]

workers_dev = false
main = "src/index.js"

rules = [
{ type = "Text", globs = [ "*/frontend/**", "*/doc/**" ], fallthrough = true }
]

#----------------------------------------
# lines below are what you should modify
#----------------------------------------

[[routes]]
# Refer to https://developers.cloudflare.com/workers/wrangler/configuration/#routes
# 1. 域名
pattern = "xxx.xxx"
custom_domain = true

[[kv_namespaces]]
binding = "PB" # do not touch this , KV name
id = "9fxxxxxxxxxxxxxxx7d" #2. KV id

[vars]
# 3. must be consistent with your routes, 与1对应
BASE_URL = "https:/xxx.xxx"

#下面的可以不改
# url to repo, displayed in index page
REPO = "https://github.com/SharzyL/pastebin-worker"

# url to favicon
FAVICON = "https://sharzy.in/favicon-32x32.png"

# the name displayed in TOS
TOS_MAINTAINER = "Paste"

# the email displayed in TOS
TOS_MAIL = "pb@shz.al"

# Cache-Control max-age for static pages
CACHE_STATIC_PAGE_AGE = 7200

# Cache-Control max-age for static pages
CACHE_PASTE_AGE = 600

2.6 Depoly

1
2
3
4
5
6
7
8
# 1. install可能报错。如果报错如下error Error: 
# https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8561120366/npm-package-cloudflare-vitest-pool-workers-5458: Request failed "404 Not Found"
# 则可以修改package.json文件下的“devDependencies”的“@cloudflare/vitest-pool-workers”属性为:
# "@cloudflare/vitest-pool-workers": "^0.4.12"
# 再次执行yarn install命令
yarn install

yarn deploy

2.7 Done

部署完后后,可以在Cloudflare后台看到一个pb的worker已经部署,访问https:/xxx.xxx成功表示部署没问题