一、示例代码
async function handleRequest(request) { const token = request.headers.get('x-custom-token'); if (token === 'token-123456') { return new Response('hello world'); } // Incorrect key supplied. Reject the request. return new Response('Sorry, you have supplied an invalid token.', { status: 403, });}addEventListener('fetch', event => { event.respondWith(handleRequest(event.request));});
二、示例预览
在浏览器地址栏中输入匹配到边缘函数触发规则的 URL,即可预览到示例效果。
·鉴权不通过,拒绝访问。
·鉴权通过,允许访问。
三、相关参考
·Runtime APIs: Headers
·Runtime APIs: Response