Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to new Deno http server, added static file router, fixed router params, added template engines #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

TKDKid1000
Copy link

@TKDKid1000 TKDKid1000 commented Jul 8, 2022

I updated the entire library to use the modern Deno http server version 0.147.0. This did involve changing the project to the fetch Request and Response under the hood, this didn't change the usage at all.

I also added a static file router, you use it via app.static("/public", "./publicdirectory"), then all files under ./publicdirectory are accessible through their url (ie. to access ./publicdirectory/style.css you go to http://localhost:8000/public/style.css)

The other change was fixing your router params system, they weren't working as expected for me (path matching worked, route parameters didn't). I switched it over to Path-to-RegExp for simpler and more familiar usage.

In the second commit, I added template engine support. It is very similar in user syntax to express, usage is as follows:

app.engine("html", htmlEngine);
app.set("views", "./views");
app.set("view engine", "html");

app.get("/engine", async (c) => {
  await c.render("test.html", {
    title: "espresso",
    content: "Welcome to espresso! Rendered with html engine.",
  });
});

Template engines should follow this interface, an example is included:

export type TemplateEngine = (
  filePath: string,
  options: Record<string, unknown>,
) => Promise<string>;

const htmlEngine: TemplateEngine = (path, options) => {
  return new Promise((res, rej) => {
    (async () => {
      try {
        let file = await Deno.readTextFile(path);
        for (const [key, value] of Object.entries(options)) {
          file = file.replaceAll(`{${key}}`, value as string);
        }
        res(file);
      } catch (err) {
        rej(err);
      }
    })();
  });
};

The other minor changes I made were:

  • The body of the context response can now be directly edited
  • The Context#json function now adds the content-type: application/json header automatically
  • Added Context#file function that sends a file to the client
  • Small QOL change: I made the "Server now listening" message send once the server is actually listening.
  • Second commit: fixed method based routing

@TKDKid1000 TKDKid1000 changed the title Updated to new Deno http server, added static file router, fixed router params Updated to new Deno http server, added static file router, fixed router params, added template engines Jul 8, 2022
@TKDKid1000
Copy link
Author

TKDKid1000 commented Jul 9, 2022

I also published a basic project website with Deno Deploy. It's currently hosted on https://espresso.deno.dev/ under their base domain. I will include the source code in either a separate pull request, or a separate repository, depending on what is wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant