Skip to Content
ExamplesRuntimesDenoBasic App

Create a Basic Deno App

A single resource answering GET /, served by Deno.serve

Steps

Folder Structure End State

    • app.ts

Install Deno

Install Deno .

Create the Following File

app.ts
import { Application, Resource, } from "jsr:@drashland/drash/modules/http.native"; // import { // Application, // Resource, // } from "npm:@drashland/drash/modules/http.native.js"; class Home extends Resource { // Create a resource. override paths = ["/"]; // Tell it which path(s) it answers to. override GET(request: Request) { // Handle GET requests to those paths. console.log(`Received request: ${request.url}`); return new Response( // This is what `app.handle()` resolves with. `Oh so easy (written at ${new Date()})`, ); } } const app = Application .builder() // Get the app's builder so we can build the app easily. .resources(Home) // Add the `Home` resource to the app. .build(); // Build the app. const hostname = "localhost"; // Define server variables for reuse below. const port = 1447; Deno.serve({ hostname, port, onListen: ({ hostname, port }) => { console.log(`\nDrash running at http://${hostname}:${port}`); }, handler: (request: Request): Promise<Response> => { return app // Let the app .handle<Response>(request) // handle the request, and .catch((error) => { // catch anything it throws. if (request.url.includes("favicon")) { return new Response(); // Browsers ask for this; ignore it. } return new Response( // Everything else gets a 500. "Sorry, but we hit an error!", { status: 500, statusText: "Internal Server Error", }, ); }); }, });

Verification

Run the Drash App

deno run --allow-net app.ts

Open It

Go to http://localhost:1447 . You should see something like the following:

Oh so easy (written at Wed Nov 01 2023 22:04:56 GMT-0400 (Eastern Daylight Time))

Notes

There is no install step for dependencies — Deno resolves the import on first run. The commented-out block shows the npm: specifier if you would rather resolve through npm, which needs the .js extension that JSR does not.

Deno provides a global URLPattern, so this uses the native entry point — see About > Concepts > Native vs. Polyfill. Its HTTP server hands you a Web Request and expects a Response back, which is exactly what the chain takes and returns, so the request goes straight through with no context object in between.

The finished app is in the repository at examples/runtimes/deno/basic-app.

Last updated on