H
Hono2d ago
igo

Help with REGEX doesnt work on route path definition

I have this route definition:
app.on(["POST", "GET"], "/api/auth/**", (c) => {
return auth.handler(c.req.raw);
});
app.on(["POST", "GET"], "/api/auth/**", (c) => {
return auth.handler(c.req.raw);
});
Its basically a listener, every path under /auth should be handled by the auth.handler. But I wanna exclude /auth/sign-up from this listener, I tried:
app.on(["POST", "GET"], "/api/auth/(?!sign-up).*", (c) => {
return auth.handler(c.req.raw);
});
app.on(["POST", "GET"], "/api/auth/(?!sign-up).*", (c) => {
return auth.handler(c.req.raw);
});
But this is not working. I also tried to add a route definition above this one, but this will make all the other routes stop working
app.post("/api/auth/sign-up", (c) => {
return c.json({
message: "Sign up successful",
});
});

// stop working
app.on(["POST", "GET"], "/api/auth/**", (c) => {
return auth.handler(c.req.raw);
});
app.post("/api/auth/sign-up", (c) => {
return c.json({
message: "Sign up successful",
});
});

// stop working
app.on(["POST", "GET"], "/api/auth/**", (c) => {
return auth.handler(c.req.raw);
});
10 Replies
ambergristle
ambergristlethis hour
the way you're using the lookahead isn't quite what you want idk if you have a /auth route, that wouldn't be covered at all but you're looking for something more like \/api(\/auth(?!\/sign-up)).* i'm not a regex wiz, so there's probably a more efficient way to solve
ambergristle
ambergristlethis hour
i find this app super helpful: https://regex101.com/
regex101
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
igo
igoOPthis hour
No description
ambergristle
ambergristlethis hour
does this meet your needs? you should double-check the edge-cases: \/api(\/auth(?!\/sign-up)).*
igo
igoOPthis hour
Still not working, idk why
ambergristle
ambergristlethis hour
ahhhhh ok, so you actually need a union group
igo
igoOPthis hour
app.on(["POST", "GET"], ["/api/auth/sign-in", "/api/auth/something", ...], (c) => {
return auth.handler(c.req.raw);
});
app.on(["POST", "GET"], ["/api/auth/sign-in", "/api/auth/something", ...], (c) => {
return auth.handler(c.req.raw);
});
I could do this, but I dont wanna pass every possible value hmmmm, what you mean? but if I use the union, i will need to pass every possible value also, right?
ambergristle
ambergristlethis hour
as a general note, it's helpful to syntax-highlight your code by adding the language after the opening backticks: ```typescript i'm talking about union capture groups (idk if that's their official name) sorry, i'm going to need a sec to track this down; it's been a while since i did regex
igo
igoOPthis hour
I think I know what u mean, but if I use the union, i will need to pass every possible value also, right?
ambergristle
ambergristlethis hour
you shouldn't gotem \/api\/auth\/(?!sign-up$).* i didn't realize you specifically wanted to exclude the /sign-up endpoint, but not any sub-routes so the union turned out to be overkill

Did you find this page helpful?