D
Deno

help

Fresh Logging Middleware

PPhonedroid6/11/2023
How would you go about writing a duration logger? Using 2 middlewares, I assumed they would behave like layers, not like a stack .. /_middleware.tsx
const before = Date.now()

const response = await
context.next()

const after = Date.now()

const delta = ( after - before )

const url = new URL(request.url)

const millis = String(delta)
.padStart(3,' ')

console.debug(`> ${ millis }ms | ${ url.pathname }`)
const before = Date.now()

const response = await
context.next()

const after = Date.now()

const delta = ( after - before )

const url = new URL(request.url)

const millis = String(delta)
.padStart(3,' ')

console.debug(`> ${ millis }ms | ${ url.pathname }`)
/a/_middleware.tsx
await new Promise(( resolve ) =>
setTimeout(resolve,2000))
await new Promise(( resolve ) =>
setTimeout(resolve,2000))
Prints the delta ( only a couple milliseconds from the page rendering ) first and afterwards whatever is in the deeper middleware .. The delay is ofc working itself ( which is observable in the browser ) Not sure if this has something to do with https://github.com/denoland/fresh/issues/1090
Yybabts6/11/2023
Route middleware | fresh docs
Add middleware routes to intercept requests or responses for analytics purposes, access control, or anything else.
Yybabts6/11/2023
if you have the same code you gave in both middlewares, accessing a/* will cause the least specific middleware to trigger first and the next middleware will trigger on ctx.next() with the way your code is written the console debug will indeed execute like a stack. Last in first out if you moved the console logging to before ctx.next() it will behave like layers because that is executed before all the more specific middleware.
PPhonedroid6/11/2023
How can I log something ( the time delta ) before it has happened lol? ( If I moved the console logging to before ctx.next() )
Yybabts6/11/2023
I'm not suggesting that you do that, from what I understand you're confused about something here and I'm not sure what. I'm confused about your confusion XD
PPhonedroid6/11/2023
I'm not confused as much as I just don't see the behavior I'm expecting as described in the documentation. Middlewares should work in a nested / layered fashion where next calls engage the next deeper middleware. As such I should be able to measure the duration of a request / the middleware the request uses in the way I did in the above example. However what I'm observing indicates that the middleware function is run completely through before the next one engages, which seems odd considering the next function is async and by it's documentation not just used to append another middleware to a stack of handlers that should be called in series. TLDR, I'm expecting nested behavior but observing stack like behavior
Yybabts6/11/2023
I did some testing with the middleware when I read your post and it worked as expected for me, do you have a repository to share that I can look at and test myself?
PPhonedroid6/11/2023
Not quite, the relevant parts are commited, however the repo on GitHub will not work out of the box, I cannot commit some required stuff atm. However if you check the /App/Source/Middleware and routes folder, you should find what you are looking for. https://github.com/PageCollective/Laser
GitHub
GitHub - PageCollective/Laser: Appoinement managment @Shopify app f...
Appoinement managment @Shopify app for Laser Hair Removal salons. - GitHub - PageCollective/Laser: Appoinement managment @Shopify app for Laser Hair Removal salons.

Looking for more? Join the community!

Recommended Posts
VS-Code auto import local modulesHey is there a way to enable some sort of auto import for local modules/files in VS-Code? Currently In memory Deno KvHi there, I am exploring with [Deno Kv](https://deno.com/deploy/docs/kv) and it looks great! But myDeno Language Server Issue - Attempting to Use KV in Existing ProjectHi! I'm attempting to create a branch of an existing project (utilizing Fresh) and add KV as an expeTwitter APIIs there a easy way to fetch Tweets using Twitter API or something else? (since they put their API cIn browser video chat client & serverHello! How might people go about architecting an in-browser video chat client & server, specificallynpm:pusher working but http://esm.sh/pusher notAny ideas? deno run -A test.js //test.js import Pusher from "pusher" // const Pusher = require('pFile Permissions on Deno.FileInfoHello. As a programming exercise I'm trying to convert a Decimal number into Octal and that Octal reDeno Fresh - Different POST handlers for different <form>s ?what's the idiomatic way of handling different POST requests on the same page?Deno.open() questionDoes, when the process terminate, deno close the files if they were opened with `Deno.open()`?Can't figure out complicated type incompatibilityAnyone know what's going on here? `T` should have resolved to `[number, number, number]` https://wwManually edit existing KV tableHello! another day, another question. Is there a way to manually edit KV database to modify an entrLooking to deploy Deno Fresh on VPS using port 443Hello! I have two questions, I used saaskit as a base. However fresh's start function is using `serTrying to use Deno.serve() for an https static hostingI was trying to setup an simple hello world response in https on my custom domain."https://esm.sh/mercadopago@1.5.15" Is not workin on deploy"https://esm.sh/mercadopago@1.5.15" Is not workin on deno deploy, any clues? it works on local but Simulating physics collisions across isolatesA bit tangential to Deno, but since Deploy uses v8 isolates, it seems that a multiplayer online gameDeno showing node globalThis errorDeno is showing this error: ```js error: Uncaught ReferenceError: __DENO_NODE_GLOBAL_THIS_0_43_0__ iHow to read Chunked request body?Hello! Is it possible to read a chunked request body using `for await`? It seems to only read up to Deno Fresh (Saaskit base) - Impossible to debug errorWhen I try to access my account by logging in, the web app becomes unusable and I get this errorPossible to deploy the chat example to deno deploy?I would like to try running this demo on deno deploy: https://deno.com/manual@v1.34.0/examples/chat`dnt` incompatible with `addEventListener`?I am using the log/mod.ts module from deno_std and running `dnt` with no special configuration as fa