新增 index.html 以請求在 CloudFront Functions 檢視器請求事件中沒有檔案名稱的 URLs - Amazon CloudFront

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

新增 index.html 以請求在 CloudFront Functions 檢視器請求事件中沒有檔案名稱的 URLs

下列程式碼範例示範如何在 CloudFront Functions 檢視器請求事件中新增 index.html 以請求沒有檔案名稱的 URLs。

JavaScript
CloudFront Functions 的 JavaScript 執行期 2.0
注意

GitHub 上提供更多範例。尋找完整的範例,並了解如何在 CloudFront Functions 範例儲存庫中設定和執行 。

async function handler(event) { var request = event.request; var uri = request.uri; // Check whether the URI is missing a file name. if (uri.endsWith('/')) { request.uri += 'index.html'; } // Check whether the URI is missing a file extension. else if (!uri.includes('.')) { request.uri += '/index.html'; } return request; }

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用 CloudFront 。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。