github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/compute/testdata/build/javascript/src/index.js (about) 1 // The entry point for your application. 2 // 3 // Use this fetch event listener to define your main request handling logic. It could be 4 // used to route based on the request properties (such as method or path), send 5 // the request to a backend, make completely new requests, and/or generate 6 // synthetic responses. 7 addEventListener('fetch', async function handleRequest(event) { 8 9 // NOTE: By default, console messages are sent to stdout (and stderr for `console.error`). 10 // To send them to a logging endpoint instead, use `console.setEndpoint: 11 // console.setEndpoint("my-logging-endpoint"); 12 13 // Get the client request from the event 14 let req = event.request; 15 16 // Make any desired changes to the client request. 17 req.headers.set("Host", "example.com"); 18 19 // We can filter requests that have unexpected methods. 20 const VALID_METHODS = ["GET"]; 21 if (!VALID_METHODS.includes(req.method)) { 22 let response = new Response("This method is not allowed", { 23 status: 405 24 }); 25 // Send the response back to the client. 26 event.respondWith(response); 27 return; 28 } 29 30 let method = req.method; 31 let url = new URL(event.request.url); 32 33 // If request is a `GET` to the `/` path, send a default response. 34 if (method == "GET" && url.pathname == "/") { 35 let headers = new Headers(); 36 headers.set('Content-Type', 'text/html; charset=utf-8'); 37 let response = new Response("<iframe src='https://developer.fastly.com/compute-welcome' style='border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%'></iframe>\n", { 38 status: 200, 39 headers 40 }); 41 // Send the response back to the client. 42 event.respondWith(response); 43 return; 44 } 45 46 // Catch all other requests and return a 404. 47 let response = new Response("The page you requested could not be found", { 48 status: 404 49 }); 50 // Send the response back to the client. 51 event.respondWith(response); 52 });