Files
PixelDocs/assets/flash_core-concepts_handlers.md.BZuBmBSX.js
2025-02-28 20:05:07 +00:00

4 lines
6.3 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import{_ as t,c as a,a0 as s,o as n}from"./chunks/framework.p2VkXzrt.js";const u=JSON.parse('{"title":"📚 Handlers","description":"","frontmatter":{"banner-title":"Flash - Handlers","banner-description":"Learn about handlers in Flash and the different types available.","head":[["meta",{"name":"twitter:image","content":"/assets/banner-cards/flash-core-concepts-handlers.png"}],["meta",{"name":"twitter:image:src","content":"https://docs.pixel-services.com/assets/banner-cards/flash-core-concepts-handlers.png"}],["meta",{"name":"twitter:card","content":"summary_large_image"}],["meta",{"name":"twitter:image:height","content":"1280"}],["meta",{"name":"twitter:image:width","content":"669"}],["meta",{"name":"twitter:description","content":""}]]},"headers":[],"relativePath":"flash/core-concepts/handlers.md","filePath":"flash/core-concepts/handlers.md"}'),i={name:"flash/core-concepts/handlers.md"};function r(o,e,l,d,h,c){return n(),a("div",null,e[0]||(e[0]=[s(`<h1 id="📚-handlers" tabindex="-1">📚 Handlers <a class="header-anchor" href="#📚-handlers" aria-label="Permalink to &quot;📚 Handlers&quot;"></a></h1><p>In Flash, handlers are the building blocks of your application logic. They are responsible for processing incoming requests, executing the necessary logic, and generating the appropriate response.</p><p>There are several types of handlers in Flash, each serving a specific purpose and providing a different level of control over the request lifecycle. Understanding the different handler types will help you structure your application logic more effectively and make the most out of Flash&#39;s powerful routing system.</p><h2 id="📦-routing-behavior" tabindex="-1">📦 Routing Behavior <a class="header-anchor" href="#📦-routing-behavior" aria-label="Permalink to &quot;📦 Routing Behavior&quot;"></a></h2><p>Before diving into the different handler types, it&#39;s essential to understand how routing works in Flash. When a request is received by the server, Flash matches the request path and method against the registered routes to find the appropriate handler. The handler is then executed, and its response is sent back to the client.</p><p>Flash supports 3 main types of routing behaviors:</p><ul><li><strong>Literal Routing</strong>: Matches the exact path specified in the route definition.</li><li><strong>Parametrized Routing</strong>: Matches paths with dynamic segments that are extracted as route parameters.</li><li><strong>Dynamic Routing</strong>: Matches any path that starts with the specified prefix and is flagged with a wildcard &quot;*&quot; character.</li></ul><h2 id="📌-handler-types" tabindex="-1">📌 Handler Types <a class="header-anchor" href="#📌-handler-types" aria-label="Permalink to &quot;📌 Handler Types&quot;"></a></h2><h3 id="_1-requesthandler" tabindex="-1">1. RequestHandler <a class="header-anchor" href="#_1-requesthandler" aria-label="Permalink to &quot;1. RequestHandler&quot;"></a></h3><p>The <code>RequestHandler</code> is the &quot;standard&quot; type of handler in Flash, it provides the most control over the request lifecycle and allows you to define custom logic for handling requests. You can extend the <code>RequestHandler</code> class to create custom handlers that process incoming requests and generate responses.</p><p>Because <code>RequestHandler</code> is an abstract class, you need to implement both the <code>handle()</code> method and the <code>super</code> constructor in your custom handler to define the logic that should be executed when a request is received.</p><p>Since <code>RequestHandler</code> is an abstract class, you can leverage and chain HDI&#39;s to create cleaner and more maintainable route logic (more on that in the Handler Default Implementations guide).</p><h3 id="_2-simplehandler" tabindex="-1">2. SimpleHandler <a class="header-anchor" href="#_2-simplehandler" aria-label="Permalink to &quot;2. SimpleHandler&quot;"></a></h3><p>The <code>SimpleHandler</code> is a lightweight handler that allows you to define request handling logic in a single method using lambda notation. It is useful for simple request processing tasks that don&#39;t require the full lifecycle control provided by <code>RequestHandler</code>.</p><p>To create a <code>SimpleHandler</code>, you can use the <code>server.get()</code>, <code>server.post()</code>, <code>server.put()</code>, <code>server.delete()</code> etc. in general, you can use the <code>server.&lt;METHOD&gt;()</code> methods to register the handler with the server.</p><p>The arguments for these methods are the route path and a lambda expression that provides the request and response objects and defines the request handling logic.</p><div class="language-java vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">server.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">get</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/hello&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, (req, res) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-&gt;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello, World!&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">});</span></span></code></pre></div><hr><p>Both <code>RequestHandler</code> and <code>SimpleHandler</code> can specify the router behavior by the naming convention of the endpoint used to register the handler.</p><ul><li><p>Literal Routing: <code>/hello</code> <br> Will match exactly <code>/hello</code></p></li><li><p>Parametrized Routing: <code>/hello/:name</code> <br> Will match <code>/hello/John</code>, <code>/hello/Alice</code>, etc.</p></li><li><p>Dynamic Routing: <code>/hello/*</code> <br> Will match <code>/hello/../..</code></p></li></ul>`,20)]))}const g=t(i,[["render",r]]);export{u as __pageData,g as default};