Search code examples
javascriptbun

Bun + stricjs application - how to add access logs / middleware functionality


I've been trying to use Bun + stricjs for a very simple web backend. And I need to execute custom code before every request is processed without having to handle the paths myself

I tried registering a custom handler but when I request to /view/file.html the code for applying business rules is not executed.

// .....
export default new Router({hostname: "0.0.0.0"})
  .use('GET', "/*", ctx => {
    console.log(`Applying super complex business rules`);
  })
  .get("/view/*", dir("./public"));

Solution

  • I'm the author of Stric. When you go to /view/index.html. The handler of /view/* is chosen because it is prioritized at that subroute.

    Also next is not a thing in Stric because it has no middleware. Middleware is affecting performance.

    If you want something to execute first you can use the guard concept. See the docs here: https://stricjs.netlify.app/#/basic/routing/main?id=guarding-routes

    Stric executes wildcards only if a handler for that specific path is not found.