Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

feat: serving static files #59

Merged
merged 4 commits into from
Mar 6, 2023
Merged

Conversation

lino-levan
Copy link
Contributor

No description provided.

import { serve } from "$std/http/server.ts";

// Here we start a simple server
console.log("Listening on http://localhost:8000");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is duplicated as serve already prints this

@kt3k
Copy link
Member

kt3k commented Feb 17, 2023

I think the example should be something like more primitive static file server without using serveFile serveDir.

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

serve(async (req: Request) => {
  const pathname = new URL(req.url).pathname;

  console.log("Got request to", pathname);

  const fileUrl = new URL("." + pathname, import.meta.url);

  try {
    const stat = await Deno.lstat(fileUrl);
    if (stat.isDirectory) {
      return new Response("is directory"); // or primitive directory listing
    }
    const file = await Deno.open(fileUrl);
    return new Response(file.readable);
  } catch (e) {
    return new Response("404: Not Found", {
      status: 404,
    });
  }
});

(We are less interested in promoting serveDir serveFile utilities in std/http/file_server.ts, but more interested in showing that we can build static file server easily using Deno APIs.

@lino-levan
Copy link
Contributor Author

Sounds good to me. I will update this PR when I get the chance. We should probably mention that the standard library has utility functions for this but otherwise agree with the point.

@lucacasonato
Copy link
Member

I don't necessarily agree we don't want to showcase std/http/file_server here. It's probably what most people should be using.

@lino-levan
Copy link
Contributor Author

Now this gets to the question of "what is deno by example for?"

My personal take is the deno by example is for beginners who want to learn the "correct" way to do a thing. If that's the view, I'd actually agree with Luca on this one. I think examples showing lower-level interactions make more sense in the deno manual instead of on the site.

That was the conclusion of this (#45) rather short-lived issue, but I'm open to other perspectives. I think by example is really important for the beginner community.

@kt3k
Copy link
Member

kt3k commented Feb 20, 2023

Ok. Then let's use utils from file_server.ts here

@deno-deploy deno-deploy bot requested a deployment to Preview February 22, 2023 07:13 Abandoned
@lino-levan
Copy link
Contributor Author

Ok. Then let's use utils from file_server.ts here

Aren't we currently using utils from file_server.ts are you recommending we use primitives instead?

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we currently using utils from file_server.ts are you recommending we use primitives instead?

It's ok as it is. LGTM

@kt3k kt3k merged commit 2313da6 into denoland:main Mar 6, 2023
@lino-levan lino-levan deleted the feat-file-server branch March 7, 2023 21:40
@lino-levan lino-levan mentioned this pull request Apr 5, 2023
44 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants