tiny-request-router icon indicating copy to clipboard operation
tiny-request-router copied to clipboard

Query parameters in path cause exception

Open dimitrovs opened this issue 4 years ago • 3 comments

I understand query parameters are not supported for routing but even using a path with them throws exception:

import { Router, Method } from 'tiny-request-router'

const router = new Router()
router.post('/v1/relays', handleAddRelay);
router.post('/?action=host', handleInitHost);

Causes:

Uncaught TypeError: Unexpected MODIFIER at 1, expected END\n  at line 21 in h\n  at line 21 in i\n  at line 21 in a\n  at line 21 in s\n  at line 21 in e._push\n  at line 21 in e.post\n  at line 1\n  at line 1 in n\n  at line 1\n  at line 1\n

dimitrovs avatar Jan 25 '21 00:01 dimitrovs

I'm also getting exception when using star wildcard

router.get("/api*", handleApiRoute);
router.get("/", handleStaticRoute);
TypeError: Unexpected MODIFIER at 4, expected END

steverandy avatar Jan 26 '21 06:01 steverandy

Same issue here. It would be awesome if the query string parameters were stripped from the URL, dumped into the response object, and routing worked as normal.

Even better would be to parse those query string parameters and put the key/value pairs into the response object.

metaskippy avatar Sep 16 '22 19:09 metaskippy

I understand query parameters are not supported for routing but even using a path with them throws exception:

import { Router, Method } from 'tiny-request-router'

const router = new Router()
router.post('/v1/relays', handleAddRelay);
router.post('/?action=host', handleInitHost);

Causes:

Uncaught TypeError: Unexpected MODIFIER at 1, expected END\n  at line 21 in h\n  at line 21 in i\n  at line 21 in a\n  at line 21 in s\n  at line 21 in e._push\n  at line 21 in e.post\n  at line 1\n  at line 1 in n\n  at line 1\n  at line 1\n

Can't you just put the router as just '/' and then get the parameters of it? router.post('/', handleInitHost);.

In the fetch event listener you would have to define the parameter const { searchParams } = new URL(request.url);, then pass it in event.respondWith(match.handler(match.params, searchParams)), then in the function you can get the parameters by using .get(parameter) on the searchParams.

bribes avatar Oct 30 '22 02:10 bribes