Custom domains let you use your own website address instead of a default one. This makes your site easier to find and looks more professional.
Custom domains in Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(handler, { hostname: "your.custom.domain", port: 80 }); async function handler(req) { return new Response("Hello from your custom domain!"); }
The hostname option sets the domain or IP address your server listens on.
Make sure your DNS settings point your custom domain to your server's IP address.
example.com at port 80 (default HTTP port).serve(handler, { hostname: "example.com", port: 80 });www.example.com at port 8080, useful for development or custom ports.serve(handler, { hostname: "www.example.com", port: 8080 });serve(handler, { hostname: "0.0.0.0", port: 80 });This program starts a simple HTTP server that listens on mycustomdomain.com at port 80. When someone visits your site, they see the message "Welcome to my custom domain site!".
Remember to set your DNS records so that mycustomdomain.com points to your server's IP address.
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; const handler = (req: Request) => { return new Response("Welcome to my custom domain site!"); }; serve(handler, { hostname: "mycustomdomain.com", port: 80 });
DNS changes can take time to update (up to 48 hours).
Use HTTPS with custom domains for security; consider using a TLS certificate provider like Let's Encrypt.
Make sure your server firewall allows traffic on the port you use (usually 80 for HTTP or 443 for HTTPS).
Custom domains let you use your own website address instead of a default URL.
Set the hostname option in Deno's serve to your domain name.
Update DNS records to point your domain to your server's IP address.
Practice
Solution
Step 1: Understand what a custom domain is
A custom domain lets you use a personalized website name instead of a default or generic address.Step 2: Identify the purpose in Deno context
Setting a custom domain in Deno allows your server to respond to your own website name, making it easier for users to find your site.Final Answer:
To use your own website name instead of a generic address -> Option BQuick Check:
Custom domain = personalized website name [OK]
- Confusing custom domain with server speed
- Thinking it updates software automatically
- Assuming it enables debugging
serve function?Solution
Step 1: Recall Deno serve options
Theservefunction accepts an option calledhostnameto set the domain name.Step 2: Match the correct option key
Onlyhostnameis the correct key; others likedomain,host, orurlare invalid here.Final Answer:
serve({ hostname: "mydomain.com" }) -> Option CQuick Check:
Use hostname option in serve [OK]
- Using 'domain' or 'host' instead of 'hostname'
- Passing the domain as a string without key
- Confusing 'url' with hostname option
import { serve } from "https://deno.land/std/http/server.ts";
serve(() => new Response("Hello"), { hostname: "example.com" });Solution
Step 1: Examine the hostname option
The code setshostname: "example.com"in the serve options.Step 2: Understand hostname resolution and binding
Deno resolves "example.com" to a public IP (93.184.216.34) via DNS. If the IP is assigned to the local machine's network interfaces, the server binds successfully and listens on that domain. Otherwise, it will throw a runtime error.Step 3: Typical behavior
Usually, binding to a hostname that resolves to a public IP not assigned locally will fail. But if the environment is configured properly (e.g., hosts file or network interface), it can succeed.Final Answer:
The server listens on example.com domain -> Option DQuick Check:
Hostname option sets server listening domain [OK]
- Thinking server listens directly on domain without IP resolution
- Assuming default localhost behavior
- Believing it falls back to all IPs
hostname: "mydomain.com" in your Deno server but users cannot reach your site. What is the most likely cause?Solution
Step 1: Verify Deno serve supports hostname
Deno'sservesupportshostnameoption, so options B, C, and D are incorrect.Step 2: Check DNS setup
If users cannot reach the site, the domain's DNS likely does not point to the server's IP, blocking access.Final Answer:
You forgot to point your domain's DNS to your server's IP address -> Option AQuick Check:
DNS must point domain to server IP [OK]
- Assuming serve lacks custom domain support
- Using wrong option key like 'host'
- Thinking Deno needs special flags for domains
Solution
Step 1: Understand hostname options
Setting hostname to "0.0.0.0" makes the server listen on all network interfaces, including your domain and localhost.Step 2: Evaluate other options
Run two servers: one with hostname "myapp.com" and one with hostname "localhost" requires two servers which is inefficient; Set hostname to "myapp.com, localhost" in a single serve call is invalid syntax; Use hostname "localhost" and rely on DNS to redirect "myapp.com" relies on DNS redirect which does not work for localhost access.Final Answer:
Set hostname to "0.0.0.0" to listen on all interfaces including domain and localhost -> Option AQuick Check:
Use 0.0.0.0 to listen on all interfaces [OK]
- Trying to list multiple hostnames in one serve call
- Running multiple servers unnecessarily
- Relying on DNS to redirect localhost
Ready to prove it?
Pass the test and this pattern moves unknown
