Routing
Path rules
Trailing slashes, decoding, route priority, 405 and the automatic OPTIONS
import { } from "@senrijs/senri";
const = ();
.("/users/me", () => "that is you"); // static segments match first
.("/users/:id", ({ }) => `user ${}`); // registration order does not matterTrailing and repeated slashes
Trailing slashes and repeated slashes are ignored. /users, /users/ and //users all match the same route.
To redirect to one form (for SEO), use trailingSlash().
Decoding
- The path is percent-decoded segment by segment. It matches a route written as is, such as
/こんにちは - A path that cannot be decoded, or that contains
%2F(an encoded/), is 400 c.pathis the normalized, decoded path. Middleware that decides by path should usec.pathorapp.use("/admin", mw), notc.req.url
Priority
Whatever the registration order, routes match in the order static → parameter → wildcard. If nothing matches down the /a/b/c branch, Senri goes back and tries /a/*.
405 and OPTIONS
When a route matches the path but not the method, the response is 405 with Allow. If OPTIONS is not registered, the response is 204 with Allow.
curl -i -X DELETE http://localhost:3927/users/1
# HTTP/1.1 405 Method Not Allowed
# allow: GET, HEAD, OPTIONSWildcard routes do not count for 405 and OPTIONS (Wildcards).