Skip to content

Routing Conventions

Use a normal file for a GET route.

routes/hello.js -> GET /hello

import { Route } from 'owebjs';
export default class HelloRoute extends Route {
handle() {
return { message: 'hello-world' };
}
}

Put parameter names in brackets.

routes/users/[id].js -> GET /users/:id

import { Route } from 'owebjs';
export default class UserRoute extends Route {
handle(req) {
return { id: req.params.id };
}
}

Use filename suffixes when an endpoint is not GET.

routes/auth/login.post.js -> POST /auth/login

Supported suffixes: .get, .post, .put, .patch, .delete

import { Route } from 'owebjs';
export default class LoginPostRoute extends Route {
handle(req, res) {
return res.status(201).send({ method: req.method, body: req.body });
}
}

Matcher params add filename-level validation.

routes/posts/[id=integer].js + matchers/integer.js

matchers/integer.js
export default function integerMatcher(value) {
return /^-?\d+$/.test(String(value));
}

Then register the matcher directory:

await app.loadRoutes({
directory: 'routes',
matchersDirectory: 'matchers',
hmr: {
enabled: true,
matchersDirectory: 'matchers',
},
});

If the matcher returns false, the route is treated as not matched.