In Astro, file-based routing automatically creates routes based on files in the src/pages folder. What is the main benefit of this approach?
Think about how routes are usually set up in traditional frameworks.
File-based routing means the framework reads your files and creates routes automatically, so you don't write route definitions yourself.
src/pages/blog/post.astro map to?Given the file src/pages/blog/post.astro in an Astro project using file-based routing, what URL path will this file serve?
File paths inside src/pages map directly to URL paths.
The folder structure inside src/pages becomes the URL path. So blog/post.astro maps to /blog/post.
In Astro's file-based routing, how do you name a file to create a dynamic route that captures a username parameter?
Dynamic route parameters are wrapped in square brackets.
Astro uses square brackets around the parameter name to define dynamic routes, like [username].astro.
src/pages/[id].astro not match /user/123 URL?You have a file src/pages/[id].astro. You try to visit /user/123 but get a 404 error. Why?
Think about how nested folders affect URL matching.
The file [id].astro matches URLs at the root level like /123. To match /user/123, you need a folder user with [id].astro inside.
Consider the file src/pages/products/[productId].astro with this code:
---
const { productId } = Astro.params;
---
Product: {productId}
What will be the rendered HTML when visiting /products/42?
---
const { productId } = Astro.params;
---
<html lang="en">
<body>
<h1>Product: {productId}</h1>
</body>
</html>Astro.params contains dynamic route parameters from the URL.
The dynamic segment [productId] captures the URL part 42. This value is available in Astro.params.productId and rendered inside the <h1> tag.
