如果有使用 cloudflare 服務,需要 serverless 功能的話,可以評估使用量來使用 cloudflare 的 workers 服務。參考:
Cloudflare Workers documentation · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.
他可以作為 client to server 的 middleware 或是當作是一個終端。 serverless 的服務各大 vps 商也都有,像是 AWS 、GCP、Azure 等,這裡也有篇 Linode 關於 Serverless Computing 的優缺點介紹。
值得一提的是他還有 wokers router 服務,以及 workers KV 服務,提供了路由和儲存的實作,有興趣可以玩玩看。另外在 workers 的設定中,如果同一個帳號中也有使用 name server 的服務,可以調整自定義網域的作法,將子域名指定過去,這樣就不用記很奇怪的域名啦。
Cloudflare 的 workers 服務有提供免費以及付費的方案,參考介紹頁面底下的表格,細節計算可以參考:
Pricing · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.
Workers 使用的語言比較偏向 nodejs ,可用的應用場景滿多的,可以參考官方文件範例。
像是如果需要做一個簡單跳轉,可以這樣寫:
const base = 'https://example.com'; const statusCode = 301; async function handleRequest(request) { const url = new URL(request.url); const { pathname, search } = url; const destinationURL = base + pathname + search; return Response.redirect(destinationURL, statusCode); } addEventListener('fetch', async event => { event.respondWith(handleRequest(event.request)); });
同場加映其他介紹文章:
Cloudflare Workers入門 ─ 簡介
Cloudflare Workers將送進來的request在cloudflare的網絡上做處理。你可以把它放在client和server的中間,用做Middleware;後方也可以沒有server ─ ...