Skip to content

Instantly share code, notes, and snippets.

@danurbanowicz
Created January 12, 2023 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danurbanowicz/a30a6e68b30fee75cfe799fc14addf97 to your computer and use it in GitHub Desktop.
Save danurbanowicz/a30a6e68b30fee75cfe799fc14addf97 to your computer and use it in GitHub Desktop.
Deno Oak Server Example
// A simple Deno Oak web server + router that parses a greeting from the URL
// and returns it as the response body
// Import the required modules
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
// Create a new instance of the Oak router
const router = new Router();
// Define our greeting route e.g. /say/{greeting}
// It's called for all HTTP verbs/requests matching the path: /say/*
router.all("/say/:greeting", (ctx, next) => {
// Return the value of `:greeting` from the parsed URL
ctx.response.body = `Deno says: ${ctx.params.greeting}`;
});
// Define a fallback route for any non-matching paths
router.get("/(.*)", (ctx, next) => {
ctx.response.body = "Enter a greeting in the URL e.g. /say/hello";
});
// Create a new instance of the Oak application
const app = new Application();
// Initalize the router middleware
app.use(router.routes());
app.use(router.allowedMethods());
// Listen to requests on the sepecified port
app.listen({ port: 8080 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment