Deno: Basic Server Example

Deno is definitely the new buzzword in the JS world lately. Creating a simple Deno server is as easy as a creating a Node.js one, and the community and support just gets bigger and bigger. Why Deno, you might ask?

Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

Or in my words: “Why not?” https://deno.land/ (official site)

So let’s get down to business:

How to install (Mac users):

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

Or:

brew install deno

Great! Now verify Deno is installed by running:

deno --help

To check the list of options and sub-options you can do with it.

Ok, let’s move on to the server in our index.ts file:

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

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

app.use(oakCors());

app.use(async (ctx) => {
const result = ctx.request.body();
ctx.response.body = await result.value;
});

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

Basically Deno loads resources once, and then caches it for any next usage.

  1. We load the Application and Router and initialise them.

2. Next we use the oakCors to allow CORS in our server, and set the routes.

3. In this example all we do is handle a POST call in the boomerang way - get the body and set the response to be exactly what we got. Just for the sake of testing.

And that’s it! All you have to do now is run your index.ts file using:

deno run index.ts

And your server is up and running, waiting for happy POST calls.

Enjoy!


Deno: Basic Server Example was originally published in Everything Full Stack on Medium, where people are continuing the conversation by highlighting and responding to this story.

Senior Frontend Engineer

Frontend Group

Thank you for your interest!

We will contact you as soon as possible.

Want to Know More?

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com
Thank you for your interest!

We will contact you as soon as possible.

Let's talk

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com