DEV Community

loading...
Cover image for five deno web frameworks - deno http

five deno web frameworks - deno http

ajcwebdev profile image anthony-campolo Updated on ・2 min read

When building a web server with NodeJS, most developers use a framework such as Express, Koa, or (the newly resurrected) Hapi. There are many frameworks currently being built for Deno that take inspiration from Node frameworks and frameworks from other languages like Flask or Sinatra.

Popularity is not always the best metric, but when it comes to open source projects it signals a greater level of developers are working with a framework which means more bug fixes, more feature requests, and more resources online to learn that framework. The five most popular frameworks right now are:

  1. Oak
  2. Drash
  3. Servest
  4. abc
  5. Opine

Craig Morten's excellent overview What Is The Best Deno Web Framework? explores these five frameworks along with a range of others.

Deno HTTP

Before diving into the frameworks it's useful to see the underlying server code they are building upon. The Deno Standard Library has an http module with a basic hello world application. First make sure you have Deno installed.

Create project directory and hello.ts

mkdir ajcwebdev-deno
cd ajcwebdev-deno
touch hello.ts
Enter fullscreen mode Exit fullscreen mode

Save the following code in hello.ts:

// hello.ts

import { serve } from "https://deno.land/std/http/server.ts"

const server = serve({ port: 8000 })

for await (const req of server) {
  req.respond({
    body: "<h1>Hello World<h1>"
  })
}
Enter fullscreen mode Exit fullscreen mode

Run Deno server

deno run --allow-net hello.ts
Enter fullscreen mode Exit fullscreen mode

Open your browser to localhost:8000

01-deno-hello-world

We can contrast this with a similar example in Node. Save the following code in a file called hello.js:

// hello.js

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Hello World</h1>');
});

server.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Run Node server

node hello.js
Enter fullscreen mode Exit fullscreen mode

Open your browser to localhost:3000.

02-node-hello-world

Discussion (0)

pic
Editor guide