Issues with Async Failure

Alastair Rosewood
StackBlitz
Node.js (forked) - StackBlitz
Starter project for Node.js, a JavaScript runtime built on Chrome's V8 JavaScript engine.
14 Replies
Hello, I’m Allie!
@Isaac McFadyen | YYZ01, just checked, and aparently the script fails on https://stackblitz.com/edit/node-t4hhov?file=src%2Fapi%2Fproviders%2FNWS%2Findex.ts, on lines 9-13, when it is just running a fetch request... Fumbling for straws here, but it might be that hono registers routes outside of the scope of a fetch request, which makes it fail?
Isaac McFadyen
Huh... yeah, that might be right? I've never used Hono TBH. I use Polka (Lukeed's package).
Isaac McFadyen
GitHub
GitHub - lukeed/polka: A micro web server so fast, it'll make you d...
A micro web server so fast, it'll make you dance! :dancers: - GitHub - lukeed/polka: A micro web server so fast, it'll make you dance!
Hello, I’m Allie!
Does it support Workers? I've seen it floating around, but I don't think I have ever actually seen someone use it in Workers...
Isaac McFadyen
Oh wait, you're correct lul the only other thing I can recommend then is itty-router but the API is different.
Hello, I’m Allie!
Yeah, the big issue for me is the fact that itty doesn't really support middleware for stuff like CORS etc. Might have to use it though...
Isaac McFadyen
Hmm... yeah it does? If you do a .any("*", () => {...add cors here}) then don't return in the handler I think?
Hello, I’m Allie!
I was looking at it, and it looks like middleware must manually be applied to each route. Let me check...
Isaac McFadyen
Oh, annoying.
Hello, I’m Allie!
So, looks like you can run middleware, but only ahead of the final route, which means that I could modify incoming data, but not the outgoing headers... So
import { Router } from 'itty-router';
const app = Router();

app.all('/', (res) => console.log('Yo'));
app.get('/*', () => new Response('Hey'));

export default {
fetch: app.handle,
};
import { Router } from 'itty-router';
const app = Router();

app.all('/', (res) => console.log('Yo'));
app.get('/*', () => new Response('Hey'));

export default {
fetch: app.handle,
};
works, but it cannot modify the final response headers.
Isaac McFadyen
Oh, interesting.
Hello, I’m Allie!
Because itty-router is so simple, the second it detects that a response is returned, it stops processing middleware, and just returns the response directly, before its headers can be modified. This is why frameworks like hono and express(I believe) support the next() function, that allows you to use a middleware before a route, intercept the response from the route, and then modify it before it is returned.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Isaac McFadyen
Oh sweet! Didn't know this, thanks!
Want results from more Discord servers?
Add your server