Back to blog

Building a REST API with Deno.js and Oak 🦕

In this tutorial, we will build a simple REST API using Deno.js and Oak. Deno is a secure runtime environment for JavaScript and TypeScript that aims to solve a number of problems found in Node.js.

Advertisement

Preparing the Environment

Before you begin, make sure Deno is installed on your system. If not, you can install it using the following command:

curl -fsSL https://deno.land/x/install/install.sh | sh

Creating the API

Let's start by creating a new file called server.ts and importing the necessary modules:

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

Now let's create the application and the router:

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

Now we can define a route for our API:

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

For our application to use the router, we add the following code:

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

Finally, we can make our application listen on port 8000:

console.log('Server running on port 8000');await app.listen({ port: 8000 });

Now, if you run deno run --allow-net server.ts, you will see that the server is running on port 8000. If you go to http://localhost:8000/api in your browser, you will see the message "Hello, Deno!".

Conclusion

Ready! You've created a simple REST API with Deno.js and Oak. Deno is still a relatively new technology, but it is already receiving a lot of support and has great potential to become a viable alternative to Node.js in certain scenarios.

To continue learning and improving your skills, check out the article on Creating a Simple Chat with Socket.IO and Node.js.

Advertisement

Let's go up 🦅

Previous post Next post

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments