HTTP โ
The following basic examples demonstrate common use cases for simple HTTP servers.
Respond with text โ
An HTTP server using the std/http
module that responds with "Hello world!" string.
CLI
Netzo
import { serve } from "https://deno.land/[email protected]/http/server.ts"
function handler(_req: Request): Response {
return new Response("Hello world!")
}
serve(handler)
import { serve } from "https://deno.land/[email protected]/http/server.ts"
function handler(_req: Request): Response {
return new Response("Hello world!")
}
serve(handler)
Deprecated Service Worker API
The fetch
event and respondWith
method were previously supported but have been deprecated in favor of the serve()
function from std/http
module (or equivalent) which provides more control over the server to handle HTTP requests.
// @deprecated - use `serve()` of the std/http module (or equivalent) instead
addEventListener('fetch', (event: FetchEvent) => {
event.respondWith(new Response('Hello world!'))
})
// @deprecated - use `serve()` of the std/http module (or equivalent) instead
addEventListener('fetch', (event: FetchEvent) => {
event.respondWith(new Response('Hello world!'))
})
Respond with JSON โ
An HTTP server using the std/http
module that responds to requests with JSON.
CLI
Netzo
import { serve } from "https://deno.land/[email protected]/http/server.ts";
function handler(_req: Request): Response {
const data = { message: "Hello world!" };
const body = JSON.stringify(data, null, 2);
return new Response(body, {
headers: { "content-type": "application/json" },
});
}
serve(handler);
import { serve } from "https://deno.land/[email protected]/http/server.ts";
function handler(_req: Request): Response {
const data = { message: "Hello world!" };
const body = JSON.stringify(data, null, 2);
return new Response(body, {
headers: { "content-type": "application/json" },
});
}
serve(handler);
Using the Response.json(body)
short-hand
Instead of manually building and returing a Response
object, the return Response.json(body)
short-hand can be used to respond directly with JSON. This will automatically stringify the body and set the content-type
header to application/json
.
function handler(_req: Request): Response {
const data = { message: 'Hello world!' }
const body = JSON.stringify(data, null, 2)
return new Response(body, {
headers: { 'content-type': 'application/json' },
})
return Response.json(data)
}
function handler(_req: Request): Response {
const data = { message: 'Hello world!' }
const body = JSON.stringify(data, null, 2)
return new Response(body, {
headers: { 'content-type': 'application/json' },
})
return Response.json(data)
}
Get client IP address โ
An HTTP server that responds to requests with the client's IP address.
CLI
Netzo
import { ConnInfo, serve } from "https://deno.land/[email protected]/http/server.ts";
function handler(_req: Request, connInfo: ConnInfo): Response {
const addr = connInfo.remoteAddr as Deno.NetAddr;
const ip = addr.hostname;
return new Response(`Your IP address is <b>${ip}</b>`, {
headers: { "content-type": "text/html" },
});
}
serve(handler);
import { ConnInfo, serve } from "https://deno.land/[email protected]/http/server.ts";
function handler(_req: Request, connInfo: ConnInfo): Response {
const addr = connInfo.remoteAddr as Deno.NetAddr;
const ip = addr.hostname;
return new Response(`Your IP address is <b>${ip}</b>`, {
headers: { "content-type": "text/html" },
});
}
serve(handler);
Proxying to other servers โ
An HTTP server that proxies requests to a different server.
CLI
Netzo
import { serve } from "https://deno.land/[email protected]/http/server.ts";
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
url.protocol = "https:";
url.hostname = "example.com";
url.port = "443";
return await fetch(url.href, {
headers: req.headers,
method: req.method,
body: req.body,
});
}
serve(handler);
import { serve } from "https://deno.land/[email protected]/http/server.ts";
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
url.protocol = "https:";
url.hostname = "example.com";
url.port = "443";
return await fetch(url.href, {
headers: req.headers,
method: req.method,
body: req.body,
});
}
serve(handler);
Wildcard Domain โ
An HTTP server that serves a wildcard domain.
CLI
Netzo
import { serve } from "https://deno.land/[email protected]/http/server.ts";
function handler(req: Request): Response {
const url = new URL(req.url);
switch (url.hostname) {
case "en.example.com":
return new Response("english");
case "es.example.com":
return new Response("espaรฑol");
default:
return new Response("default");
}
}
serve(handler);
import { serve } from "https://deno.land/[email protected]/http/server.ts";
function handler(req: Request): Response {
const url = new URL(req.url);
switch (url.hostname) {
case "en.example.com":
return new Response("english");
case "es.example.com":
return new Response("espaรฑol");
default:
return new Response("default");
}
}
serve(handler);