Skip to content

Commit 4b079a7

Browse files
docs
1 parent 019d6a7 commit 4b079a7

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

docs/canary/concepts/app.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ const app = new App()
1717
app.listen();
1818
```
1919

20+
All items are applied from top to bottom. This means that when you defined a
21+
middleware _after_ a `.get()` handler, it won't be included.
22+
23+
```ts
24+
const app = new App()
25+
.use((ctx) => {
26+
// Will be called for all middlewares
27+
return ctx.next();
28+
})
29+
.get("/", () => new Response("hello"))
30+
.use((ctx) => {
31+
// Will only be called for `/about
32+
return ctx.next();
33+
})
34+
.get("/about", (ctx) => ctx.render(<h1>About me</h1>));
35+
```
36+
2037
## `.use()`
2138

2239
Add one or more [middlewares](/docs/canary/concepts/middleware). Middlewares are
@@ -42,6 +59,15 @@ Adding middlewares at a specific path:
4259
app.use("/foo/bar", middleware);
4360
```
4461

62+
Middlewares can also be instantiated lazily:
63+
64+
```ts
65+
app.use("/foo/bar", async () => {
66+
const mod = await import("./path/to/my/middleware.ts");
67+
return mod.default;
68+
});
69+
```
70+
4571
## `.get()`
4672

4773
Respond to a `GET` request with the specified middlewares.
@@ -60,6 +86,15 @@ app.get("/about", middleware1, middleware2, async (ctx) => {
6086
});
6187
```
6288

89+
You can also pass lazy middlewares:
90+
91+
```ts
92+
app.get("/about", async () => {
93+
const mod = await import("./middleware-or-handler.ts");
94+
return mod.default;
95+
});
96+
```
97+
6398
## `.post()`
6499

65100
Respond to a `POST` request with the specified middlewares.
@@ -80,6 +115,15 @@ app.post("/api/user/:id", middleware1, middleware2, async (ctx) => {
80115
});
81116
```
82117

118+
You can also pass lazy middlewares:
119+
120+
```ts
121+
app.post("/api/user/:id", async () => {
122+
const mod = await import("./middleware-or-handler.ts");
123+
return mod.default;
124+
});
125+
```
126+
83127
## `.put()`
84128

85129
Respond to a `PUT` request with the specified middlewares.
@@ -100,6 +144,15 @@ app.put("/api/user/:id", middleware1, middleware2, async (ctx) => {
100144
});
101145
```
102146

147+
You can also pass lazy middlewares:
148+
149+
```ts
150+
app.put("/api/user/:id", async () => {
151+
const mod = await import("./middleware-or-handler.ts");
152+
return mod.default;
153+
});
154+
```
155+
103156
## `.delete()`
104157

105158
Respond to a `DELETE` request with the specified middlewares.
@@ -120,6 +173,15 @@ app.delete("/api/user/:id", middleware1, middleware2, async (ctx) => {
120173
});
121174
```
122175

176+
You can also pass lazy middlewares:
177+
178+
```ts
179+
app.delete("/api/user/:id", async () => {
180+
const mod = await import("./middleware-or-handler.ts");
181+
return mod.default;
182+
});
183+
```
184+
123185
## `.head()`
124186

125187
Respond to a `HEAD` request with the specified middlewares.
@@ -138,6 +200,15 @@ app.head("/api/user/:id", middleware1, middleware2, async (ctx) => {
138200
});
139201
```
140202

203+
You can also pass lazy middlewares:
204+
205+
```ts
206+
app.head("/api/user/:id", async () => {
207+
const mod = await import("./middleware-or-handler.ts");
208+
return mod.default;
209+
});
210+
```
211+
141212
## `.all()`
142213

143214
Respond to a request for all HTTP verbs with the specified middlewares.
@@ -156,6 +227,34 @@ app.all("/api/foo", middleware1, middleware2, async (ctx) => {
156227
});
157228
```
158229

230+
You can also pass lazy middlewares:
231+
232+
```ts
233+
app.all("/api/foo", async () => {
234+
const mod = await import("./middleware-or-handler.ts");
235+
return mod.default;
236+
});
237+
```
238+
239+
## `.fsRoute()`
240+
241+
Injects all file-based routes, middlewares, layouts and error pages to the app
242+
instance.
243+
244+
```ts
245+
app.fsRoutes();
246+
```
247+
248+
You can optionally pass a path where they should be mounted.
249+
250+
```ts
251+
app.fsRoutes("/foo/bar");
252+
```
253+
254+
> [info]: If possible, routes are lazily loaded. Routes that set a route config
255+
> and set `routeOverride` in particular, are never lazily loaded as Fresh would
256+
> need to load the file to get the route pattern.
257+
159258
## `.route()`
160259

161260
TODO

0 commit comments

Comments
 (0)