0
0
DenoHow-ToBeginner · 3 min read

How to Use Oak Framework in Deno: Simple Guide

To use the oak framework in Deno, import it from its URL, create a new Application, define routes with Router, and start the server with app.listen(). Oak helps build web servers easily by handling requests and responses.
📐

Syntax

The basic syntax to use Oak involves importing Application and Router from Oak, creating an app instance, defining routes, and starting the server.

  • import: Load Oak modules from URL.
  • Application: Main server object.
  • Router: Defines URL routes and handlers.
  • app.use(): Connect middleware or router.
  • app.listen(): Start server on a port.
typescript
import { Application, Router } from "https://deno.land/x/oak@v12.5.0/mod.ts";

const app = new Application();
const router = new Router();

router.get('/', (context) => {
  context.response.body = 'Hello Oak!';
});

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });
💻

Example

This example creates a simple Oak server that responds with 'Hello Oak!' at the root URL. It shows how to set up routes and start the server.

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

const app = new Application();
const router = new Router();

router.get('/', (context) => {
  context.response.body = 'Hello Oak!';
});

app.use(router.routes());
app.use(router.allowedMethods());

console.log('Server running on http://localhost:8000');
await app.listen({ port: 8000 });
Output
Server running on http://localhost:8000
⚠️

Common Pitfalls

Common mistakes when using Oak include:

  • Not importing from the correct Oak URL or version.
  • Forgetting to use router.allowedMethods() which handles HTTP method responses properly.
  • Not awaiting app.listen(), which blocks the server from running.
  • Not calling app.use() with router middleware, so routes don’t work.
typescript
/* Wrong: Missing allowedMethods and not awaiting listen */
import { Application, Router } from "https://deno.land/x/oak@v12.5.0/mod.ts";

const app = new Application();
const router = new Router();

router.get('/', (context) => {
  context.response.body = 'Hello Oak!';
});

app.use(router.routes());
// Missing app.use(router.allowedMethods());

app.listen({ port: 8000 }); // Missing await

/* Right: */

app.use(router.allowedMethods());
await app.listen({ port: 8000 });
📊

Quick Reference

Here is a quick summary of key Oak components and methods:

FeatureDescription
ApplicationMain server instance to handle requests and responses
RouterDefines URL routes and HTTP methods
router.get(path, handler)Handles GET requests at given path
app.use(middleware)Adds middleware or router to the app
app.listen({ port })Starts the server on specified port
router.allowedMethods()Handles HTTP method responses (405, 501)

Key Takeaways

Import Oak modules from the official URL with version for stability.
Create an Application and Router to define routes and start the server.
Always use router.allowedMethods() with router.routes() for proper HTTP handling.
Await app.listen() to keep the server running.
Check for correct URL imports and middleware usage to avoid common errors.