DEV Community

Cover image for Basic Routing App in Deno using Oak
Haaris Iqubal for Recoding

Posted on • Edited on

2 1

Basic Routing App in Deno using Oak

Routing your Internet application is one of most important thing developer has to know about, Routing help use to keep our application safe as we can pass many authentication middleware, Routing also help our Internet application to have different pages for different purposes. Sometime implementing routing become more tedious job to implement if we want to use internal modules so how we will implement that let us think about!!! As Deno support Third Party module like Oak which will provide us a “application layer” as well as “routing layer”. With using these two classes we can implement our routing application. So without further ado let’s just do it. First of all we need to create our “app.ts” file our working directory will look like this.

-app.ts

Now we have to import our module we gonna use Oak so we will copy its link and write our import statement.

import { Application, Router } from "https://deno.land/x/oak/mod.ts";

After importing our Application Class and Router Class we now need to initialize both of our module using following statement.

const app = new Application();

const router = new Router();

After initializing our router we can now setup what request we want to do with server and how will actually the router and what function they gonna provide. Request can be of many type like “GET”, “POST” ,“DELETE” etc…

router
  .get("/",(ctx) => {
   ctx.response.body = "Router has been created";
   // Implement your code
   })
  .post("/addPost", (ctx) => {
   ctx.response.body = "This is port request";
   // Implement your code   
   });

Our router has been setup now we have then these router path to our application, we can implement this by passing our router as a middleware to our application.

app.use(router.routes());

app.use(router.allowedMethods());

For the final step we just need to make our server listen to Port 8000 or any thing you want.

app.listen({port: 8000});

So this is how we can build a Deno Routing application pretty easily. Our final code will look this in app.ts file.

import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
const router = new Router();
router
  .get("/",(ctx) => {
   ctx.response.body = "Router has been created";
   // Implement your code
   })
  .post("/addPost", (ctx) => {
   ctx.response.body = "This is port request";
   // Implement your code   
   });
app.use(router.routes());
app.use(router.allowedMethods());;
app.listen({port: 8000});

So have fun with Deno and enjoy Deno with Oak 👍.

Alt Text

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (1)

Collapse
 
ben profile image
Ben Halpern

Nice post!

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Discover more in this insightful article and become part of the thriving DEV Community. Developers at every level are welcome to share and enrich our collective expertise.

A simple “thank you” can brighten someone’s day. Please leave your appreciation in the comments!

On DEV, sharing skills lights our way and strengthens our connections. Loved the read? A quick note of thanks to the author makes a real difference.

Count me in