Created
January 12, 2023 16:54
-
-
Save danurbanowicz/a30a6e68b30fee75cfe799fc14addf97 to your computer and use it in GitHub Desktop.
Deno Oak Server Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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