Back to Blog
DenoOakTypeScriptBackendAPI

Developing Modern REST APIs with Deno and Oak

How to build secure and efficient APIs with Deno, a modern alternative to Node.js, and its popular middleware framework, Oak?

Eren ÖzdemirApril 15, 20248 min

What is Deno?

Deno is a secure runtime for JavaScript and TypeScript, developed by Ryan Dahl, the original creator of Node.js. It runs in a secure sandbox by default, meaning your code has no access to the file system, network, or environment unless explicitly permitted. It also supports TypeScript out of the box and has a decentralized package management system.

Oak: The Deno Equivalent of Express.js

Oak is a middleware framework for Deno, inspired by Koa and Express.js. It allows developers to easily manage routing, middleware, and the HTTP request/response cycle for APIs and web applications.

An Example API Server

Creating a simple 'Hello World' server with Oak is quite straightforward:

import { Application, Router } from "https://deno.land/x/oak/mod.ts";\n\nconst router = new Router();\nrouter.get("/", (ctx) => {\n  ctx.response.body = "Hello Deno and Oak!";\n});\n\nconst app = new Application();\napp.use(router.routes());\napp.use(router.allowedMethods());\n\nconsole.log("Server running on http://localhost:8000");\nawait app.listen({ port: 8000 });

To run this code, you need to allow network access due to Deno's security features: deno run --allow-net app.ts

Conclusion

Deno and Oak offer a powerful duo for building modern, secure, and efficient backend services. Its TypeScript support and security-focused approach make it a noteworthy option, especially for new projects. Although the maturity of the Node.js ecosystem is not yet present in Deno, it is promising with its rapidly growing community.

Did you enjoy this article?

Share it to help more people discover it.