Functions Framework 可讓您編寫簡易函式,並在許多不同環境中執行,包括:
- Cloud Functions
- 您的本機開發電腦
- Cloud Run 和 Cloud Run on GKE
- 以 Knative 為基礎的環境
例如,在 Node.js 10 中,這個架構可讓您以下列內容為基礎:
exports.helloWorld = (req, res) => {
res.send('Hello, World');
};
產生以下內容:
curl http://my-url
# Output: Hello, World
而不必費心編寫 HTTP 伺服器,或擔心複雜的要求處理邏輯。
功能
- 啟動本機開發服務器以快速進行測試
- 呼叫函式以回應要求
- 自動將符合 CloudEvents 規格的事件解除封送
- 可在不同無服務器平台使用
快速入門導覽課程
使用以下內容建立 index.js
檔案:
exports.helloWorld = (req, res) => {
res.send('Hello, World');
};
如要在本機執行函式,請先使用 npm init
建立 package.json
檔案:
npm init
接著安裝 Functions Framework:
npm install @google-cloud/functions-framework
在 package.json
中新增 start
指令碼,並透過指令列引數傳送設定:
"scripts": {
"start": "functions-framework --target=helloWorld"
}
使用 npm start
啟動內建本機開發服務器:
npm start
...
Serving function...
Function: helloWorld
URL: http://localhost:8080/
透過其他終端機視窗使用 curl
,傳送要求至此函式:
curl localhost:8080
# Output: Hello, World
瞭解詳情
您可以前往 GitHub 頁面閱讀更詳細的說明文件,瞭解如何使用 Functions Framework。