D
Deno

help

In Deno, what is the best way to simply host static websites?

Aabcdef389/16/2023
What is the best way to simply host static/cached files? Without having to do a read file call every request. And would this work with Deno Deploy? Also how much can I optimise
Deno.serve((_req) => {
return new Response("Hello, World!");
})
Deno.serve((_req) => {
return new Response("Hello, World!");
})
, I want the highest possible requests/second.
Aabcdef389/16/2023
Thanks, but that is not really what I'm looking for. It doesn't do caching or anything like that. I'm better off implementing a system myself maybe. But what about getting the highest number of requests/second locally? Bun.serve can do 2x-3x the amount of requests/second, which can't be right.
Aabcdef389/16/2023
Homebrew Formulae
wrk
Homebrew’s package index
IioB9/16/2023
it does do caching
Aabcdef389/17/2023
How?
import { serveDir, serveFile } from "https://deno.land/std@0.194.0/http/file_server.ts";
Deno.serve((req: Request) => {
const pathname = new URL(req.url).pathname;

if (pathname === "/simple_file") {
return serveFile(req, "./path/to/file.txt");
}

if (pathname.startsWith("/static")) {
return serveDir(req, {
fsRoot: "public",
urlRoot: "static",
});
}

return new Response("404: Not Found", {
status: 404,
});
});
import { serveDir, serveFile } from "https://deno.land/std@0.194.0/http/file_server.ts";
Deno.serve((req: Request) => {
const pathname = new URL(req.url).pathname;

if (pathname === "/simple_file") {
return serveFile(req, "./path/to/file.txt");
}

if (pathname.startsWith("/static")) {
return serveDir(req, {
fsRoot: "public",
urlRoot: "static",
});
}

return new Response("404: Not Found", {
status: 404,
});
});
IioB9/17/2023
It sets the appropriate headers automatically to ensure that a browser will not download the same file twice Behind the scenes, serveDir and the CLI that https://deno.land/std/http/file_server.ts provides does the same thing, so you can't go wrong either way.
Aabcdef389/17/2023
That is not really the same thing, but okay. That's just status code 304.
IioB9/17/2023
It's more complicated than that unfortunately. serveDir supports a bunch of caching headers, including https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
ETag - HTTP | MDN
The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed. Additionally, etags help to prevent simultaneous updates of a resource from overwriting each othe...
Aabcdef389/17/2023
Oh, okay. But status 304 works. Well, I only know of it because I accidentally used it and all my code changes did nothing. It took me a while to realise this.

Looking for more? Join the community!

Recommended Posts
zsh: command not found: deno on macOSplease send helpUsing esbuild with import mapsHello 👋 I'm attempting to migrate one of my front-end projects to using Deno for tooling instead ofproxy-agent not working on Deno DeployI'm trying to use a proxy for the node-fetch requests in a deno app. I chose proxy-agent npm packagExtremely high CPU usage in Windows running on Intel Pentium processorI have a simple app (about 300 lines of code, few deps). I compile it into a Windows executable. WheImpossible to use `"jsx": "preserve"` in deno.json...Well it seems that I can't use jsx preserve in deno.json, when I try to run any tasks in deno.json, integrating deno_webOk I seem to have poobrain today. Can't seem to get the deno_web extension working. The core versioCan I use Workers in an executable ?Everything is in the questions, on the doc site, it is mention that we can't use Workers when creatiWalkSync dosnt get filesIm trying to read all files of a folder with WalkSync from the deno STD libary but it dosnt see thedeno behaving differently in local dev env and production docker buildsI'm currently trying to switch from using node+yarn 2 for my blogs static files to deno. however theFront end dev needed for 6 months.Hello. I am in need of a frontend developer. dm me pls.Include platform and version in compiled binary filenameRight now when i run `deno compile --output bin/myprogram`, it works as expected. Is is possible to How to handle a user abort by Deno.serveHello, I have a question and discovered this Discord channel now. The Deno website has an example atAPI hooks during restart for deno run --watchHello! I thought it would be super fun to further minimize repetitive tasks while manually testing adeno compile -A npm:cowsay not workingHello, I am trying `deno compile -A npm:cowsay` and then following that with `./cowsay Hello World!`compiling issue with import { WebSocketServer } from "npm:ws@^8.14.1"Using this impot in a "deno run --unstable [etc...]" env works flawless / as intended but trying to Is there any way to get Deno to not statically analyze import()?I understand that statically analyzing and fetching dynamic import() calls is a good thing for perfoUsing Deno in Frontend.Hey, I was wandering if it is possible to use Deno for Vanilla Frontend development. I dont want to Excluding certain directories when using "walk"Hello, I'm writing a script that should crawl through a directory and search for a given text insideSyntax Error: Unexpected token 'export'Hello, We are using Supabase Deno Edge Functions and are getting this error while trying to import aDeno Deploy QuestionsHey there. We are using Supabase to run edge functions, which in turn built their feature on top of