DEV Community

Dario Castañé
Dario Castañé

Posted on

1

Simple Deno HTTP server running in Nanos unikernel

This short post shows how to get a self-executable Deno HTTP server to run in Nanos using Ops.

The server

const port = Number(Deno.env.get('PORT'));
const listener = Deno.listen({ port: port });

console.log(`http://localhost:${port}`);

for await (const conn of listener) {
  (async () => {
    const requests = Deno.serveHttp(conn);
    for await (const { respondWith } of requests) {
      respondWith(new Response("Hello world"));
    }
  })();
}
Enter fullscreen mode Exit fullscreen mode

Compilation and local deployment

deno compile --allow-env --allow-net hello.ts
ops run -e HOME=/ -e PORT=8000 -p 8000 hello
Enter fullscreen mode Exit fullscreen mode

The trick is to set the HOME variable. It can have any value, but / feels right.

Summary

In this post, we have seen a minimal Deno HTTP server working in a Nanos unikernel.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay