0
0
DenoHow-ToBeginner ยท 3 min read

How to Use HTTP Server in Deno: Simple Guide with Example

In Deno, you can create an HTTP server using the serve function from the std/http/server.ts module. Import serve, define a handler function for requests, and call serve with that handler to start listening for HTTP requests.
๐Ÿ“

Syntax

The basic syntax to create an HTTP server in Deno involves importing the serve function, defining a request handler, and calling serve with that handler. The handler receives a Request object and returns a Response object.

  • import { serve }: Brings the server function from Deno's standard library.
  • async function handler(req): Handles incoming requests.
  • serve(handler, { port }): Starts the server on the specified port.
typescript
import { serve } from "https://deno.land/std@0.203.0/http/server.ts";

async function handler(req: Request): Promise<Response> {
  return new Response("Hello from Deno HTTP server!", {
    status: 200,
    headers: { "content-type": "text/plain" },
  });
}

serve(handler, { port: 8000 });
๐Ÿ’ป

Example

This example shows a simple HTTP server that responds with a plain text greeting to every request. It listens on port 8000 and sends back a 200 OK status with a message.

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

async function handler(req: Request): Promise<Response> {
  return new Response("Hello from Deno HTTP server!", {
    status: 200,
    headers: { "content-type": "text/plain" },
  });
}

console.log("HTTP server running on http://localhost:8000/");
serve(handler, { port: 8000 });
Output
HTTP server running on http://localhost:8000/
โš ๏ธ

Common Pitfalls

Some common mistakes when using the Deno HTTP server include:

  • Not importing serve from the correct URL or version.
  • Forgetting to return a Response object from the handler.
  • Not specifying the port or using a port already in use.
  • Not running the script with the --allow-net permission, which is required to listen on network ports.

Always run your server with deno run --allow-net your_server.ts to avoid permission errors.

typescript
/* Wrong: Missing return of Response */
import { serve } from "https://deno.land/std@0.203.0/http/server.ts";

async function handler(req: Request) {
  // No return here
}

serve(handler, { port: 8000 });

/* Right: Return a Response */
async function correctHandler(req: Request): Promise<Response> {
  return new Response("OK", { status: 200 });
}
๐Ÿ“Š

Quick Reference

Remember these key points when using Deno's HTTP server:

  • Import serve from https://deno.land/std@0.203.0/http/server.ts with a version.
  • Handler functions must return a Response object.
  • Use serve(handler, { port }) to start the server.
  • Run with --allow-net permission.
โœ…

Key Takeaways

Use the serve function from Deno's standard library to create an HTTP server.
Your request handler must return a Response object to reply to clients.
Always run your server script with --allow-net permission to allow network access.
Specify the port in serve options to control where the server listens.
Check for common mistakes like missing return statements or incorrect imports.