kwhitley / itty-router

A little router.
MIT License
1.7k stars 77 forks source link

ES6 Module Syntax TypeErrors with minimal example #146

Closed MexHigh closed 1 year ago

MexHigh commented 1 year ago

I've tried using this minimal example mentioned in the README file:

import { Router } from 'itty-router'

const router = Router()

router.get("/", (request, env, context) => {
    return new Response("Test")
})

export default {
    fetch: router.handle
}

But when running wrangler dev and hitting the / route, the console shows this error:

image

However, the route returns the correct result "Test".

How do I get rid of these errors?

capsiamese commented 1 year ago
// 404 for everything else
router.all('*', () => new Response('Not Found.', { status: 404 }))
kwhitley commented 1 year ago
// 404 for everything else
router.all('*', () => new Response('Not Found.', { status: 404 }))

Yeah, that request for the favicon.ico is certainly triggering that, but it appears to also be on the 200... I'll look into this one!

dotysan commented 1 year ago

In addition to the catchall 404 at the end mentioned above, I often have something like this toward the top of my router worker:

const HT_NC= {status: 204};

  .get('/favicon.ico', ()=> {
    return new Response(null, HT_NC)
  })

Of course, you could also return a valid icon if you prefer.

kwhitley commented 1 year ago

Closing this as answered!