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
servefrom the correct URL or version. - Forgetting to return a
Responseobject from the handler. - Not specifying the port or using a port already in use.
- Not running the script with the
--allow-netpermission, 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
servefromhttps://deno.land/std@0.203.0/http/server.tswith a version. - Handler functions must return a
Responseobject. - Use
serve(handler, { port })to start the server. - Run with
--allow-netpermission.
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.