hviana / faster

A fast and optimized middleware server with an absurdly small amount of code (300 lines) built on top of Deno's native HTTP APIs with no dependencies. It also has a collection of useful middlewares: log file, serve static, CORS, session, rate limit, token, body parsers, redirect, proxy and handle upload. In "README" there are examples of all the resources. Faster's ideology is: all you need is an optimized middleware manager, all other functionality is middleware.
MIT License
36 stars 8 forks source link

How to serve static files? #4

Open dion-sony opened 1 year ago

dion-sony commented 1 year ago
import {
  req,
  res,
  Server,
  serveStatic,
} from "https://deno.land/x/faster/mod.ts";

const port = parseInt(Deno.env.get("PORT") || "8080");

const server = new Server();

server.get(
    "/*",
    serveStatic("./dist"),
  );

console.log(`🚀 Listening on port ${port}: http://localhost:${port}`);
await server.listen({ port});

./dist is a regular web app, with ./dist/index.html and other assets. This content is served by other file servers no problem.

This fails: with the browser pointing to http://localhost:3000

❯ PORT=3000 deno run --allow-all --unstable server.ts
🚀 Listening on port 3000: http://localhost:3000
Error: Is a directory (os error 21)
    at async read (ext:deno_io/12_io.js:108:17)
    at async Object.pull (https://deno.land/std@0.184.0/streams/readable_stream_from_reader.ts:59:22)
hviana commented 1 year ago

You are just pointing the static files directory. The system does not know that the route "/" is equivalent to "index.html". You also have to indicate the root route. I believe this might work: server.get( "/", async (ctx: any, next: any) => { ctx.redirect("/index.html"); await next(); }, );

hviana commented 1 year ago

Or, also, using the redirection middleware: server.get( "/", redirect("/Index.html"), );