You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/basics/routes.md
+72-1Lines changed: 72 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ sidebar_position: 1
4
4
5
5
# Routes π
6
6
7
+
## Overview β¨
8
+
7
9
In Dart Frog, a route consists of an `onRequest` function (called a route handler) exported from a `.dart` file in the `routes` directory. Each endpoint is associated with a routes file based on its file name. Files named, `index.dart` will correspond to a `/` endpoint.
8
10
9
11
For example, if you create `routes/hello.dart` that exports an `onRequest` method like below, it will be accessible at `/hello`.
All route handlers have access to a `RequestContext` which can be used to access the incoming request as well as dependencies provided to the request context (see middleware).
21
+
## Request Context π
22
+
23
+
All route handlers have access to a `RequestContext` which can be used to access the incoming request as well as dependencies provided to the request context ([see middleware](/docs/basics/middleware)).
When defining routes, it's possible to encounter route conflicts.
123
+
124
+
A route conflict occurs when more than one route handler resolves to the same endpoint.
125
+
126
+
For example, given the following file structure:
127
+
128
+
```
129
+
βββ routes
130
+
βΒ Β βββ api
131
+
βΒ Β βΒ Β βββ index.dart
132
+
βΒ Β βββ api.dart
133
+
```
134
+
135
+
Both `routes/api/index.dart` and `routes/api.dart` resolve the the `/api` endpoint.
136
+
137
+
When running the development server via `dart_frog dev`, Dart Frog will report route conflicts while the development server is running. You can resolve the conflicts and hot reload will allow you to continue development without having to restart the server.
138
+
139
+
```bash
140
+
[hotreload] - Application reloaded.
141
+
142
+
Route conflict detected. `routes/api.dart` and `routes/api/index.dart` both resolve to `/api`.
143
+
```
144
+
145
+
When generating a production build via `dart_frog build`, Dart Frog will report all detected route conflicts and fail the build if one or more route conflicts are detected.
146
+
147
+
## Rogue Routes π₯·
148
+
149
+
Similar to route conflicts, it's also possible to run into rogue routes when working with Dart Frog.
150
+
151
+
A route is considered rogue when it is defined outside of an existing subdirectory with the same name.
152
+
153
+
For example:
154
+
155
+
```
156
+
βββ routes
157
+
β βββ api
158
+
β β βββ example.dart
159
+
β βββ api.dart
160
+
```
161
+
162
+
In the above scenario, `routes/api.dart` is rogue because it is defined outside of the existing `api` directory.
163
+
164
+
To correct this, `api.dart` should be renamed to `index.dart` and placed within the `api` directory like:
165
+
166
+
```
167
+
βββ routes
168
+
β βββ api
169
+
β β βββ example.dart
170
+
β β βββ index.dart
171
+
```
172
+
173
+
When running the development server via `dart_frog dev`, Dart Frog will report rogue routes while the development server is running. You can resolve the issues and hot reload will allow you to continue development without having to restart the server.
174
+
175
+
```bash
176
+
[hotreload] - Application reloaded.
177
+
178
+
Rogue route detected. `routes/api.dart` should be renamed to `routes/api/index.dart`.
179
+
```
180
+
181
+
When generating a production build via `dart_frog build`, Dart Frog will report all detected rogue routes and fail the build if one or more rogue routes are detected.
0 commit comments