Request Chain

What Is This?

The Request Chain module provides you with code to create HTTP applications. It follows the concepts discussed on the Concepts > Chains page.

If you have used previous versions of Drash, you will notice that this chain is essentially Drash v1.x and Drash v2.x, but in a more portable format.

Syntax

Chain + Resource

Creating a Request Chain with a single resource looks like the code below. You will notice the code is almost 100% the same across runtimes. This is a result of Drash being runtime agnostic — meaning you can use the code in any runtime that can run JavaScript.

deno.ts
import {
  Chain,
  Resource,
} from "https://esm.sh/@drashland/drash/modules/chains/RequestChain/mod.native.js";
 
// Create a resource
class Home extends Resource {
  paths = ["/"];
 
  GET(request: Request) {
    console.log(`Received request: ${request.url}`);
    return new Response(
      `Hello from Home.GET()! (written at ${new Date()})`,
    );
  }
}
 
// Build the chain and add the resource
const chain = Chain
  .builder()
  .resources(Home)
  .build();

I have a chain variable. Now what?

The highlighted chain variable above is what you would use in your chosen runtime's HTTP server to process requests. Specifically, you would call its .handle() method (this is the only method it has). Examples of using chain.handle() are shown below.

Chain + Resource + Server

Taking the above code and plugging it into a runtime's HTTP server looks like:

deno.ts
import {
  Chain,
  Resource,
} from "https://esm.sh/@drashland/drash/modules/chains/RequestChain/mod.native.js";
 
// import {
//   Chain,
//   Resource,
// } from "npm:@drashland/drash/modules/chains/RequestChain/mod.native.js";
 
// Create a resource
class Home extends Resource {
  paths = ["/"];
 
  GET(request: Request) {
    console.log(`Received request: ${request.url}`);
    return new Response(
      `Hello from Home.GET()! (written at ${new Date()})`,
    );
  }
}
 
// Build the chain and add the resource
const chain = Chain
  .builder()
  .resources(Home)
  .build();
 
// Define server variables for reuse below
const hostname = "localhost";
const port = 1447;
 
// Create and start the server
Deno.serve({
  hostname,
  port,
  onListen: ({ hostname, port }) => {
    console.log(`\nDrash running at http://${hostname}:${port}`);
  },
  handler: (request: Request): Promise<Response> => {
    // Pass the request to the chain
    return chain
      .handle<Response>(request)
      .catch((error) => {
        if (request.url.includes("favicon")) {
          return new Response();
        }
 
        return new Response(
          "Sorry, but we hit an error!",
          {
            status: 500,
            statusText: "Internal Server Error",
          },
        );
      });
  },
});
 

Promises

This chain uses Promise objects under the hood. This means you can use it with the following:

Code is provided below to give you an idea of how you could use .then() and await with this chain in Deno v1.37.x.

chain_with_then.ts
import {
  Chain
} from "npm:@drashland/drash@v3.0.0-beta.1/modules/chains/RequestChain/mod.native";
 
// Create a resource
class Home extends Resource {
  public paths = ["/"];
  public GET(request: Request) {
    return new Response(`Hello!`);
  }
}
 
// Build the chain
const chain = Chain
  .builder()
  .resources(Home)
  .build();
 
// The chain in this Deno server uses `.then()`
Deno.serve({
    port: 1447,
    hostname: "localhost",
    handler: (request: Request) => { // `async` IS NOT needed here because `await` is not used
      return chain
        .handle(request)
        .then((response) => {
          console.log({ response });
          return response;
        });
    },
});

Next Steps

Feel free to follow our recommendation or navigate the documentation pages at your leisure.

Our Recommendations