Master the routing system and create dynamic routes.
CanxJS uses a Radix Tree based router, making it incredibly fast. Let's dive into defining routes.
Routes are defined on the app instance:
app.get("/posts", getPosts);
app.post("/posts", createPost);
app.put("/posts/:id", updatePost);
app.delete("/posts/:id", deletePost);You can capture values from the URL using the colon syntax:
app.get("/users/:id", (req, res) => {
const userId = req.params.id;
return res.send(`User ID: ${userId}`);
});Organize your routes using groups. This is great for APIs:
app.group("/api/v1", (router) => {
router.get("/users", listUsers);
router.get("/products", listProducts);
});This creates /api/v1/users and /api/v1/products.
Have questions?
Join the discussion on GitHub