This topic provides sample code that shows how to identify request information.

Sample code

You can configure different policies for request information, such as the requested URL, request method, User-Agent, and IP address. The following sample code provides an example on how requests are rejected based on the configured policies:

async function handleRequest(request) {

  const url = new URL(request.url)

  // Reject requests to access a file with the .txt or .mp4 extension.
  const extRegExp = new RegExp(/\.(txt|mp4)$/)

  if (extRegExp.test(url.pathname)) {
    return new Response("403", { status: 403 })

  // Return different content to POST requests.
  if (request.method === "POST") {
    return new Response("POST Request")

  // Reject requests with a specific User-Agent header.
  const ua = request.headers.get("User-Agent") || ""
  if (ua.includes("value")) {
    return new Response("403", { status: 403 })

  // Reject requests from a specified IP address. You must read the value of Ali-Cdn-Real-Ip to obtain the real IP address of the client.
  const clientIP = request.headers.get("Ali-Cdn-Real-Ip")
  if (clientIP === "") {
    return new Response("403", { status: 403 })

  return fetch(request)

addEventListener("fetch", event => {


Response to requests that meet the configured policiesExample 1
Response to normal requestsExample 2