@modern-js/main-doc 2.58.3 → 2.60.0
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/_meta.json +10 -5
- package/docs/en/apis/app/hooks/api/lambda.mdx +4 -48
- package/docs/en/apis/app/hooks/api/middleware.mdx +11 -0
- package/docs/en/apis/app/runtime/core/use-loader.mdx +1 -1
- package/docs/en/community/blog/v2-release-note.mdx +1 -1
- package/docs/en/components/enable-bff.mdx +19 -2
- package/docs/en/components/extend-bff-function.mdx +5 -0
- package/docs/en/components/init-app.mdx +0 -1
- package/docs/en/components/init-rspack-app.mdx +0 -1
- package/docs/en/components/other-plugins.mdx +0 -0
- package/docs/en/components/ssr-monitor.mdx +3 -0
- package/docs/en/configure/app/auto-load-plugin.mdx +4 -0
- package/docs/en/configure/app/output/ssg.mdx +52 -141
- package/docs/en/configure/app/plugins.mdx +2 -2
- package/docs/en/configure/app/tools/esbuild.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +1 -1
- package/docs/en/configure/app/tools/tailwindcss.mdx +1 -1
- package/docs/en/guides/_meta.json +0 -5
- package/docs/en/guides/advanced-features/_meta.json +3 -8
- package/docs/en/guides/advanced-features/bff/_meta.json +1 -1
- package/docs/en/guides/advanced-features/bff/extend-server.mdx +154 -0
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +52 -123
- package/docs/en/guides/advanced-features/bff/function.mdx +108 -80
- package/docs/en/guides/advanced-features/bff/sdk.mdx +40 -51
- package/docs/en/guides/advanced-features/build-performance.mdx +6 -21
- package/docs/en/guides/advanced-features/page-performance/_meta.json +1 -0
- package/docs/en/guides/advanced-features/rspack-start.mdx +6 -14
- package/docs/en/guides/basic-features/_meta.json +31 -9
- package/docs/en/guides/basic-features/css/_meta.json +1 -0
- package/docs/en/guides/basic-features/css/css-in-js.mdx +34 -0
- package/docs/en/guides/basic-features/{css-modules.mdx → css/css-modules.mdx} +0 -4
- package/docs/en/guides/basic-features/css/css.mdx +25 -0
- package/docs/en/guides/basic-features/{css.mdx → css/tailwindcss.mdx} +5 -66
- package/docs/en/guides/basic-features/data/data-fetch.mdx +134 -235
- package/docs/en/guides/basic-features/data/data-write.mdx +66 -77
- package/docs/en/guides/basic-features/debug/_meta.json +1 -0
- package/docs/en/guides/basic-features/debug/rsdoctor.mdx +57 -0
- package/docs/en/guides/{advanced-features → basic-features/debug}/using-storybook.mdx +2 -0
- package/docs/en/guides/basic-features/render/_meta.json +1 -0
- package/docs/en/guides/basic-features/render/ssg.mdx +208 -0
- package/docs/en/guides/{advanced-features/ssr/cache.mdx → basic-features/render/ssr-cache.mdx} +38 -50
- package/docs/en/guides/basic-features/render/ssr.mdx +301 -0
- package/docs/en/guides/basic-features/render/streaming-ssr.mdx +230 -0
- package/docs/en/guides/basic-features/routes.mdx +274 -263
- package/docs/en/guides/basic-features/static-assets/_meta.json +1 -0
- package/docs/en/guides/basic-features/static-assets.mdx +2 -2
- package/docs/en/guides/basic-features/testing/_meta.json +1 -0
- package/docs/en/guides/basic-features/testing/cypress.mdx +95 -0
- package/docs/en/guides/basic-features/testing/jest.mdx +148 -0
- package/docs/en/guides/basic-features/testing/playwright.mdx +111 -0
- package/docs/en/guides/basic-features/testing/vitest.mdx +100 -0
- package/docs/en/guides/concept/entries.mdx +9 -2
- package/docs/en/guides/deprecated.md +2 -0
- package/docs/en/guides/get-started/quick-start.mdx +1 -1
- package/docs/en/guides/get-started/tech-stack.mdx +4 -4
- package/docs/en/guides/topic-detail/_meta.json +0 -6
- package/docs/en/guides/topic-detail/generator/create/config.mdx +0 -10
- package/docs/en/guides/topic-detail/generator/create/use.mdx +0 -1
- package/docs/en/plugin/_meta.json +19 -0
- package/docs/en/plugin/cli-plugins/_meta.json +1 -0
- package/docs/en/plugin/cli-plugins/plugin-bff.mdx +5 -0
- package/docs/en/plugin/cli-plugins/plugin-ssg.mdx +5 -0
- package/docs/en/{guides/rsbuild-plugins → plugin/cli-plugins}/plugin-swc.mdx +7 -0
- package/docs/en/plugin/cli-plugins/plugin-tailwind.mdx +5 -0
- package/docs/en/plugin/cli-plugins.mdx +6 -0
- package/docs/en/{guides/advanced-features/rsbuild-plugin.mdx → plugin/introduction.mdx} +36 -11
- package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/extend.mdx +1 -1
- package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/implement.mdx +3 -3
- package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/plugin-api.mdx +2 -2
- package/docs/en/plugin/rsbuild-plugins/_meta.json +1 -0
- package/docs/en/plugin/rsbuild-plugins.mdx +3 -0
- package/docs/en/tutorials/first-app/c03-css.mdx +1 -1
- package/docs/zh/_meta.json +10 -5
- package/docs/zh/apis/app/hooks/api/lambda.mdx +5 -48
- package/docs/zh/apis/app/hooks/api/middleware.mdx +11 -0
- package/docs/zh/apis/app/runtime/core/use-loader.mdx +1 -1
- package/docs/zh/community/blog/v2-release-note.mdx +1 -1
- package/docs/zh/components/enable-bff.mdx +19 -2
- package/docs/zh/components/extend-bff-function.mdx +5 -0
- package/docs/zh/components/init-app.mdx +0 -1
- package/docs/zh/components/init-rspack-app.mdx +0 -1
- package/docs/zh/components/other-plugins.mdx +0 -0
- package/docs/zh/components/ssr-monitor.mdx +3 -0
- package/docs/zh/configure/app/auto-load-plugin.mdx +4 -0
- package/docs/zh/configure/app/output/ssg.mdx +49 -139
- package/docs/zh/configure/app/plugins.mdx +2 -2
- package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/configure/app/tools/tailwindcss.mdx +1 -1
- package/docs/zh/guides/_meta.json +0 -5
- package/docs/zh/guides/advanced-features/_meta.json +3 -8
- package/docs/zh/guides/advanced-features/bff/_meta.json +1 -1
- package/docs/zh/guides/advanced-features/bff/extend-server.mdx +156 -0
- package/docs/zh/guides/advanced-features/bff/frameworks.mdx +51 -117
- package/docs/zh/guides/advanced-features/bff/function.mdx +69 -59
- package/docs/zh/guides/advanced-features/bff/sdk.mdx +27 -36
- package/docs/zh/guides/advanced-features/build-performance.mdx +6 -21
- package/docs/zh/guides/advanced-features/page-performance/_meta.json +1 -0
- package/docs/zh/guides/advanced-features/rspack-start.mdx +8 -17
- package/docs/zh/guides/basic-features/_meta.json +31 -9
- package/docs/zh/guides/basic-features/alias.mdx +5 -11
- package/docs/zh/guides/basic-features/css/_meta.json +1 -0
- package/docs/zh/guides/basic-features/css/css-in-js.mdx +34 -0
- package/docs/zh/guides/basic-features/css/css.mdx +25 -0
- package/docs/zh/guides/basic-features/{css.mdx → css/tailwindcss.mdx} +3 -64
- package/docs/zh/guides/basic-features/data/data-fetch.mdx +96 -211
- package/docs/zh/guides/basic-features/data/data-write.mdx +54 -55
- package/docs/zh/guides/basic-features/debug/_meta.json +1 -0
- package/docs/zh/guides/basic-features/debug/rsdoctor.mdx +57 -0
- package/docs/zh/guides/{advanced-features → basic-features/debug}/using-storybook.mdx +1 -1
- package/docs/zh/guides/basic-features/env-vars.mdx +1 -1
- package/docs/zh/guides/basic-features/render/_meta.json +1 -0
- package/docs/zh/guides/basic-features/render/ssg.mdx +210 -0
- package/docs/zh/guides/{advanced-features/ssr/cache.mdx → basic-features/render/ssr-cache.mdx} +16 -26
- package/docs/zh/guides/basic-features/render/ssr.mdx +309 -0
- package/docs/zh/guides/{advanced-features/ssr/stream.mdx → basic-features/render/streaming-ssr.mdx} +22 -37
- package/docs/zh/guides/basic-features/routes.mdx +251 -237
- package/docs/zh/guides/basic-features/static-assets/_meta.json +1 -0
- package/docs/zh/guides/basic-features/static-assets.mdx +3 -7
- package/docs/zh/guides/basic-features/testing/_meta.json +1 -0
- package/docs/zh/guides/basic-features/testing/cypress.mdx +95 -0
- package/docs/zh/guides/basic-features/testing/jest.mdx +148 -0
- package/docs/zh/guides/basic-features/testing/playwright.mdx +112 -0
- package/docs/zh/guides/basic-features/testing/vitest.mdx +100 -0
- package/docs/zh/guides/concept/entries.mdx +6 -3
- package/docs/zh/guides/deprecated.md +4 -0
- package/docs/zh/guides/get-started/quick-start.mdx +1 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +8 -8
- package/docs/zh/guides/topic-detail/_meta.json +0 -6
- package/docs/zh/guides/topic-detail/generator/create/config.mdx +0 -10
- package/docs/zh/guides/topic-detail/generator/create/use.mdx +0 -1
- package/docs/zh/plugin/_meta.json +19 -0
- package/docs/zh/plugin/cli-plugins/_meta.json +1 -0
- package/docs/zh/plugin/cli-plugins/plugin-bff.mdx +5 -0
- package/docs/zh/plugin/cli-plugins/plugin-ssg.mdx +5 -0
- package/docs/zh/{guides/rsbuild-plugins → plugin/cli-plugins}/plugin-swc.mdx +7 -0
- package/docs/zh/plugin/cli-plugins/plugin-tailwind.mdx +5 -0
- package/docs/zh/plugin/cli-plugins.mdx +6 -0
- package/docs/zh/{guides/advanced-features/rsbuild-plugin.mdx → plugin/introduction.mdx} +38 -13
- package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/extend.mdx +1 -1
- package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/implement.mdx +3 -3
- package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/plugin-api.mdx +2 -2
- package/docs/zh/plugin/rsbuild-plugins/_meta.json +1 -0
- package/docs/zh/plugin/rsbuild-plugins.mdx +4 -0
- package/docs/zh/tutorials/first-app/c03-css.mdx +1 -1
- package/i18n.json +42 -6
- package/package.json +7 -6
- package/rspress.config.ts +1 -58
- package/src/components/Footer/index.tsx +1 -1
- package/src/pages/index.tsx +0 -1
- package/docs/en/apis/app/hooks/api/api.mdx +0 -80
- package/docs/en/apis/app/hooks/api/app.mdx +0 -12
- package/docs/en/apis/app/hooks/config/storybook.mdx +0 -37
- package/docs/en/guides/advanced-features/bff/type.mdx +0 -46
- package/docs/en/guides/advanced-features/eslint.mdx +0 -148
- package/docs/en/guides/advanced-features/ssg.mdx +0 -116
- package/docs/en/guides/advanced-features/ssr/_meta.json +0 -1
- package/docs/en/guides/advanced-features/ssr/index.mdx +0 -23
- package/docs/en/guides/advanced-features/ssr/stream.mdx +0 -248
- package/docs/en/guides/advanced-features/ssr/usage.mdx +0 -341
- package/docs/en/guides/advanced-features/ssr.mdx +0 -555
- package/docs/zh/apis/app/hooks/api/api.mdx +0 -81
- package/docs/zh/apis/app/hooks/api/app.mdx +0 -12
- package/docs/zh/apis/app/hooks/config/storybook.mdx +0 -38
- package/docs/zh/guides/advanced-features/bff/type.mdx +0 -46
- package/docs/zh/guides/advanced-features/eslint.mdx +0 -152
- package/docs/zh/guides/advanced-features/ssg.mdx +0 -116
- package/docs/zh/guides/advanced-features/ssr/_meta.json +0 -1
- package/docs/zh/guides/advanced-features/ssr/index.mdx +0 -23
- package/docs/zh/guides/advanced-features/ssr/usage.mdx +0 -329
- /package/docs/en/guides/advanced-features/{bff/index.mdx → bff.mdx} +0 -0
- /package/docs/en/guides/advanced-features/{code-split.mdx → page-performance/code-split.mdx} +0 -0
- /package/docs/en/guides/advanced-features/{inline-assets.mdx → page-performance/inline-assets.mdx} +0 -0
- /package/docs/en/guides/advanced-features/{optimize-bundle.mdx → page-performance/optimize-bundle.mdx} +0 -0
- /package/docs/en/guides/basic-features/{mock.mdx → debug/mock.mdx} +0 -0
- /package/docs/en/guides/basic-features/{proxy.mdx → debug/proxy.mdx} +0 -0
- /package/docs/en/guides/basic-features/{json-files.mdx → static-assets/json-files.mdx} +0 -0
- /package/docs/en/guides/basic-features/{svg-assets.mdx → static-assets/svg-assets.mdx} +0 -0
- /package/docs/en/guides/basic-features/{wasm-assets.mdx → static-assets/wasm-assets.mdx} +0 -0
- /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/_meta.json +0 -0
- /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook-list.mdx +0 -0
- /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook.mdx +0 -0
- /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/introduction.mdx +0 -0
- /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/lifecycle.mdx +0 -0
- /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/relationship.mdx +0 -0
- /package/docs/en/{guides → plugin}/rsbuild-plugins/plugin-esbuild.mdx +0 -0
- /package/docs/zh/guides/advanced-features/{bff/index.mdx → bff.mdx} +0 -0
- /package/docs/zh/guides/advanced-features/{code-split.mdx → page-performance/code-split.mdx} +0 -0
- /package/docs/zh/guides/advanced-features/{inline-assets.mdx → page-performance/inline-assets.mdx} +0 -0
- /package/docs/zh/guides/advanced-features/{optimize-bundle.mdx → page-performance/optimize-bundle.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{css-modules.mdx → css/css-modules.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{mock.mdx → debug/mock.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{proxy.mdx → debug/proxy.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{json-files.mdx → static-assets/json-files.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{svg-assets.mdx → static-assets/svg-assets.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{wasm-assets.mdx → static-assets/wasm-assets.mdx} +0 -0
- /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/_meta.json +0 -0
- /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook-list.mdx +0 -0
- /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook.mdx +0 -0
- /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/introduction.mdx +0 -0
- /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/lifecycle.mdx +0 -0
- /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/relationship.mdx +0 -0
- /package/docs/zh/{guides → plugin}/rsbuild-plugins/plugin-esbuild.mdx +0 -0
@@ -0,0 +1,154 @@
|
|
1
|
+
# Extend BFF Server
|
2
|
+
|
3
|
+
In some applications, developers may want to handle all BFF functions uniformly, such as for authentication, logging, data processing, etc.
|
4
|
+
|
5
|
+
Modern.js provides two methods that allow developers to extend the BFF Server freely according to the runtime framework.
|
6
|
+
|
7
|
+
## Middleware
|
8
|
+
|
9
|
+
Developers can write middleware in the `api/_app.ts` file to extend the BFF Server. Taking Express as the runtime framework, here is an example of how to write a middleware to add permission checks:
|
10
|
+
|
11
|
+
```ts title="api/_app.ts"
|
12
|
+
import { hook } from '@modern-js/runtime/server';
|
13
|
+
import { Request, Response, NextFunction } from 'express';
|
14
|
+
|
15
|
+
export default hook(({ addMiddleware }) => {
|
16
|
+
addMiddleware(async (req: Request, res: Response, next: NextFunction) => {
|
17
|
+
if (req.url !== '/api/login') {
|
18
|
+
const sid = req?.cookies?.sid;
|
19
|
+
if (!sid) {
|
20
|
+
res.status(400);
|
21
|
+
res.json({ code: -1, message: 'need login' });
|
22
|
+
} else {
|
23
|
+
next();
|
24
|
+
}
|
25
|
+
} else {
|
26
|
+
next();
|
27
|
+
}
|
28
|
+
});
|
29
|
+
});
|
30
|
+
```
|
31
|
+
|
32
|
+
Then add a standard BFF function `api/lambda/hello.ts`:
|
33
|
+
|
34
|
+
```ts title="api/lambda/hello.ts"
|
35
|
+
export default async () => {
|
36
|
+
return 'Hello Modern.js';
|
37
|
+
};
|
38
|
+
```
|
39
|
+
|
40
|
+
Next, in the frontend, add the code to access the API in `src/routes/page.tsx`, directly using the integrated method for the call:
|
41
|
+
|
42
|
+
```ts title="src/routes/page.tsx"
|
43
|
+
import { useState, useEffect } from 'react';
|
44
|
+
import { get as hello } from '@api/hello';
|
45
|
+
|
46
|
+
export default () => {
|
47
|
+
const [text, setText] = useState('');
|
48
|
+
|
49
|
+
useEffect(() => {
|
50
|
+
async function fetchMyApi() {
|
51
|
+
const { message } = await hello();
|
52
|
+
setText(message);
|
53
|
+
}
|
54
|
+
|
55
|
+
fetchMyApi();
|
56
|
+
}, []);
|
57
|
+
|
58
|
+
return <p>{text}</p>;
|
59
|
+
};
|
60
|
+
```
|
61
|
+
|
62
|
+
Now run the `dev` command to start the project, and visit `http://localhost:8080/`. You will find that the request to `/api/hello` is intercepted:
|
63
|
+
|
64
|
+

|
65
|
+
|
66
|
+
Finally, modify the frontend code in `src/routes/page.tsx` to call the login interface before accessing `/api/hello`:
|
67
|
+
|
68
|
+
:::note
|
69
|
+
Here the login interface is not actually implemented; the code is just for demonstration.
|
70
|
+
:::
|
71
|
+
|
72
|
+
```ts
|
73
|
+
import { useState, useEffect } from 'react';
|
74
|
+
import { get as hello } from '@api/hello';
|
75
|
+
import { post as login } from '@api/login';
|
76
|
+
|
77
|
+
export default () => {
|
78
|
+
const [text, setText] = useState('');
|
79
|
+
|
80
|
+
useEffect(() => {
|
81
|
+
async function fetchAfterLogin() {
|
82
|
+
const { code } = await login();
|
83
|
+
if (code === 0) {
|
84
|
+
const { message } = await hello();
|
85
|
+
setText(message);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
fetchAfterLogin();
|
89
|
+
}, []);
|
90
|
+
|
91
|
+
return <p>{text}</p>;
|
92
|
+
};
|
93
|
+
```
|
94
|
+
|
95
|
+
Refresh the page to see that the access to `/api/hello` is successful:
|
96
|
+
|
97
|
+

|
98
|
+
|
99
|
+
The above code simulates adding middleware in `api/_app.ts` to implement simple login functionality. Similarly, other functionalities can be implemented in this hook file to extend the BFF Server.
|
100
|
+
|
101
|
+
:::info
|
102
|
+
The way middleware is written may differ depending on the runtime framework. For details, see [Runtime Frameworks](/guides/advanced-features/bff/frameworks).
|
103
|
+
:::
|
104
|
+
|
105
|
+
## Define Server Instance
|
106
|
+
|
107
|
+
In addition to middleware, you can also define a BFF Server instance via the `api/app.ts`. Developers need to export an instance that can be recognized by the runtime framework plugin. Here is a simple demonstration of how to define a server instance with Koa and Express.
|
108
|
+
|
109
|
+
import { Tabs, Tab as TabItem } from "@theme";
|
110
|
+
|
111
|
+
<Tabs>
|
112
|
+
<TabItem value="express" label="Express.js" default>
|
113
|
+
|
114
|
+
```ts title="api/app.ts"
|
115
|
+
import express from 'express';
|
116
|
+
|
117
|
+
const app = express();
|
118
|
+
app.use(async (req, res, next) => {
|
119
|
+
console.info(`access url: ${req.url}`);
|
120
|
+
next();
|
121
|
+
});
|
122
|
+
|
123
|
+
export default app;
|
124
|
+
```
|
125
|
+
|
126
|
+
</TabItem>
|
127
|
+
<TabItem value="koa" label="Koa.js">
|
128
|
+
|
129
|
+
```ts title="api/app.ts"
|
130
|
+
import koa from 'koa';
|
131
|
+
|
132
|
+
const app = new Koa();
|
133
|
+
app.use(async (ctx, next) => {
|
134
|
+
console.info(`access url: ${ctx.url}`);
|
135
|
+
await next();
|
136
|
+
});
|
137
|
+
|
138
|
+
export default app;
|
139
|
+
```
|
140
|
+
|
141
|
+
</TabItem>
|
142
|
+
</Tabs>
|
143
|
+
|
144
|
+
In complex BFF logic, defining a server instance allows for easier organization of code logic and directory structure design through a single entry point. In this file, you can perform initialization logic, add global middleware, declare routes, and even extend the original framework.
|
145
|
+
|
146
|
+
The routes defined by the BFF functions will be registered after the routes defined in the `app.ts` file. Therefore, you can also intercept the routes defined by the BFF functions here for preprocessing or early responses.
|
147
|
+
|
148
|
+
:::note
|
149
|
+
At this time, if `api/_app.ts` exists in the application, the defined middleware will be executed after the middleware exported by the `api/app.ts` instance. In most cases, middleware can cover most customization needs for BFF functions. Only when the application's server-side logic is more complex do you need to customize the server instance.
|
150
|
+
:::
|
151
|
+
|
152
|
+
:::caution
|
153
|
+
When `api/app.ts` does not exist in the application, Modern.js will default to adding [koa-body](https://www.npmjs.com/package/koa-body). When `api/app.ts` does exist in the application, if developers wish to use BFF functions with bodies, they will need to **add `koa-body` themselves**.
|
154
|
+
:::
|
@@ -1,152 +1,81 @@
|
|
1
|
-
|
2
|
-
sidebar_position: 3
|
3
|
-
---
|
1
|
+
# Runtime Framework
|
4
2
|
|
5
|
-
|
3
|
+
Modern.js BFF supports different runtime frameworks. Currently, Modern.js BFF supports two runtime frameworks: [Express.js](https://expressjs.com/) and [Koa.js](https://koajs.com/).
|
6
4
|
|
7
|
-
|
5
|
+
When using different runtime frameworks, some APIs may differ.
|
8
6
|
|
9
|
-
##
|
7
|
+
## Accessing Request Context
|
10
8
|
|
11
|
-
|
9
|
+
In BFF functions, you may need to access the request context to handle more logic. Depending on the runtime framework, you need to use different APIs:
|
12
10
|
|
13
|
-
|
14
|
-
import { hook } from '@modern-js/runtime/server';
|
15
|
-
import { Request, Response, NextFunction } from 'express';
|
11
|
+
import { Tabs, Tab as TabItem } from "@theme";
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
if (req.url !== '/api/login') {
|
20
|
-
const sid = req?.cookies?.sid;
|
21
|
-
if (!sid) {
|
22
|
-
res.status(400);
|
23
|
-
res.json({ code: -1, message: 'need login' });
|
24
|
-
} else {
|
25
|
-
next();
|
26
|
-
}
|
27
|
-
} else {
|
28
|
-
next();
|
29
|
-
}
|
30
|
-
});
|
31
|
-
});
|
32
|
-
```
|
33
|
-
|
34
|
-
Then add a normal BFF function `/api/hello.ts`:
|
13
|
+
<Tabs>
|
14
|
+
<TabItem value="express" label="Express.js" default>
|
35
15
|
|
36
|
-
```ts
|
37
|
-
|
38
|
-
|
16
|
+
```ts title="api/lambda/hello.ts"
|
17
|
+
import { useContext } from '@modern-js/runtime/express'
|
18
|
+
export const get = async () => {
|
19
|
+
const { req } = useContext();
|
20
|
+
console.info(`access url: ${req.url}`);
|
21
|
+
return 'Hello Modern.js'
|
39
22
|
};
|
40
23
|
```
|
41
24
|
|
42
|
-
|
25
|
+
</TabItem>
|
26
|
+
<TabItem value="koa" label="Koa.js">
|
43
27
|
|
44
|
-
```ts
|
45
|
-
import {
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
useEffect(() => {
|
52
|
-
async function fetchMyApi() {
|
53
|
-
const { message } = await hello();
|
54
|
-
setText(message);
|
55
|
-
}
|
56
|
-
|
57
|
-
fetchMyApi();
|
58
|
-
}, []);
|
59
|
-
|
60
|
-
return <p>{text}</p>;
|
28
|
+
```ts title="api/lambda/hello.ts"
|
29
|
+
import { useContext } from '@modern-js/runtime/koa'
|
30
|
+
export const get = async () => {
|
31
|
+
const ctx = useContext();
|
32
|
+
console.info(`access url: ${req.url}`);
|
33
|
+
return 'Hello Modern.js'
|
61
34
|
};
|
62
35
|
```
|
63
36
|
|
64
|
-
|
37
|
+
</TabItem>
|
38
|
+
</Tabs>
|
65
39
|
|
66
|
-
|
40
|
+
:::info
|
41
|
+
For more details, refer to [useContext](/apis/app/runtime/bff/use-context).
|
42
|
+
:::
|
67
43
|
|
68
|
-
|
44
|
+
## Using Middleware
|
69
45
|
|
70
|
-
|
71
|
-
import { useState, useEffect } from 'react';
|
72
|
-
import { get as hello } from '@api/hello';
|
73
|
-
import { post as login } from '@api/login';
|
46
|
+
In BFF functions, you may need to use middleware to handle more logic. Depending on the runtime framework, you need to use different APIs:
|
74
47
|
|
75
|
-
|
76
|
-
|
48
|
+
<Tabs>
|
49
|
+
<TabItem value="express" label="Express.js" default>
|
77
50
|
|
78
|
-
|
79
|
-
|
80
|
-
const { code } = await login();
|
81
|
-
if (code === 0) {
|
82
|
-
const { message } = await hello();
|
83
|
-
setText(message);
|
84
|
-
}
|
85
|
-
}
|
86
|
-
fetchAfterLogin();
|
87
|
-
}, []);
|
51
|
+
```ts title="api/_app.ts"
|
52
|
+
import { hook } from '@modern-js/runtime/express';
|
88
53
|
|
89
|
-
|
90
|
-
|
54
|
+
export default hook(({ addMiddleware }) => {
|
55
|
+
addMiddleware((req, res, next) => {
|
56
|
+
req.query.id = 'koa';
|
57
|
+
next();
|
58
|
+
});
|
59
|
+
});
|
91
60
|
```
|
92
61
|
|
93
|
-
|
94
|
-
|
95
|
-

|
96
|
-
|
97
|
-
The above code simulates the way to add middleware to the `/api/_app.ts` to achieve an easy login function. Also, other functions can be implemented in this hook file to extend BFF Server.
|
98
|
-
|
99
|
-
## Framework Mode
|
100
|
-
|
101
|
-
Under the framework writing, Modern.js does not collect middleware in the `api/_app.ts`, and the running process is controlled by the plugin itself.
|
102
|
-
|
103
|
-
### Express
|
62
|
+
</TabItem>
|
63
|
+
<TabItem value="koa" label="Koa.js">
|
104
64
|
|
105
|
-
|
65
|
+
```ts title="api/_app.ts"
|
66
|
+
import { hook } from '@modern-js/runtime/koa';
|
106
67
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
const app = express();
|
113
|
-
|
114
|
-
app.put('/user', function (req, res) {
|
115
|
-
res.send('Got a PUT request at /user');
|
116
|
-
});
|
117
|
-
|
118
|
-
app.use(async (req, res, next) => {
|
119
|
-
console.info(`access url: ${req.url}`);
|
120
|
-
next();
|
68
|
+
export default hook(({ addMiddleware }) => {
|
69
|
+
addMiddleware(async (ctx, next) => {
|
70
|
+
ctx.req.query.id = 'koa';
|
71
|
+
await next();
|
72
|
+
});
|
121
73
|
});
|
122
|
-
|
123
|
-
export default app;
|
124
74
|
```
|
125
75
|
|
126
|
-
|
127
|
-
|
128
|
-
The Koa framework is written in a similar way to Express. It supports defining the startup logic of API Server in `app.[tj]s`, performing the initialization work of the application, adding global middleware, declaring routes, extending the original framework, etc.
|
129
|
-
|
130
|
-
The route defined by the BFF function will be registered after the route defined by the `app.ts` file, so here you can also intercept the route defined by the BFF function, preprocess or respond in advance.
|
131
|
-
|
132
|
-
:::caution
|
133
|
-
Use the framework writing, when there is no `app.ts`, Modern.js will add koa-body by default. When there is `app.ts`, if the developer wants to use the BFF function with Body, he needs to ensure that the koa-body middleware has been added.
|
76
|
+
</TabItem>
|
77
|
+
</Tabs>
|
134
78
|
|
79
|
+
:::info
|
80
|
+
For more details, refer to [hook](/apis/app/runtime/bff/hook).
|
135
81
|
:::
|
136
|
-
|
137
|
-
```ts title=api/app.ts
|
138
|
-
import koa from 'koa';
|
139
|
-
|
140
|
-
const app = new Koa();
|
141
|
-
|
142
|
-
app.put('/user', function (req, res) {
|
143
|
-
res.send('Got a PUT request at /user');
|
144
|
-
});
|
145
|
-
|
146
|
-
app.use(async (ctx, next) => {
|
147
|
-
console.info(`access url: ${ctx.url}`);
|
148
|
-
await next();
|
149
|
-
});
|
150
|
-
|
151
|
-
export default app;
|
152
|
-
```
|