Update application.md

This commit is contained in:
Pearl C 2022-12-25 12:29:19 +08:00 committed by GitHub
parent df4b42308c
commit cb5d324800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,16 @@
# Application # Application
## 概览 ## Overview
### Web服务 ### Web Service
Nocobase Application 是基于 [Koa](https://koajs.com/) 实现的 WEB 框架,兼容 Koa 的 API。
NocoBase Application is a web framework implemented based on [Koa](https://koajs.com/), compatible with Koa API.
```javascript ```javascript
// index.js // index.js
const { Application } = require('@nocobase/server'); const { Application } = require('@nocobase/server');
// 创建App实例并配置数据库连接信息 // Create App instance and configure the database
const app = new Application({ const app = new Application({
database: { database: {
dialect: 'sqlite', dialect: 'sqlite',
@ -17,24 +18,25 @@ const app = new Application({
} }
}); });
// 注册中间件 响应请求 // Register middleware, response to requests
app.use(async ctx => { app.use(async ctx => {
ctx.body = 'Hello World'; ctx.body = 'Hello World';
}); });
// 以命令行模式启动 // Run in the CLI mode
app.runAsCLI(); app.runAsCLI();
``` ```
在命令行中运行 `node index.js start` 启动服务后,使用 `curl` 请求服务。 After running `node index.js start` in CLI to start service, use `curl` to request service.
```bash ```bash
$> curl localhost:3000 $> curl localhost:3000
Hello World Hello World
``` ```
### 命令行工具 ### CLI Tool
Nocobase Application 中也内置了 `cli commander`,可以当作命令行工具运行。
NocoBase Application has a built-in `cli commander`, which can be run as CLI tool.
```javascript ```javascript
// cmd.js // cmd.js
@ -53,22 +55,21 @@ app.cli.command('hello').action(async () => {
app.runAsCLI() app.runAsCLI()
``` ```
在命令行中运行 Run in CLI:
```bash ```bash
$> node cmd.js hello $> node cmd.js hello
hello world hello world
``` ```
### 插件注入 ### Inject Plugin
Nocobase Application 被设计为高度可扩展的框架,可以编写插件注入到应用中扩展功能。 NocoBase Application is designed as a highly extensible framework, plugins can be written and injected to extend the functionality of the application. For example, the above-mentioned Web service can be replaced with a plugin.
例如上面的 Web 服务可以替换为插件形式。
```javascript ```javascript
const { Application, Plugin } = require('@nocobase/server'); const { Application, Plugin } = require('@nocobase/server');
// 通过继承 Plugin 类来编写插件 // Write plugin by inheriting the Plugin class
class HelloWordPlugin extends Plugin { class HelloWordPlugin extends Plugin {
load() { load() {
this.app.use(async (ctx, next) => { this.app.use(async (ctx, next) => {
@ -84,53 +85,53 @@ const app = new Application({
} }
}); });
// 注入插件 // Inject plugin
app.plugin(HelloWordPlugin, { name: 'hello-world-plugin'} ); app.plugin(HelloWordPlugin, { name: 'hello-world-plugin'} );
app.runAsCLI() app.runAsCLI()
``` ```
### 更多示例 ### More Examples
更加详细的插件开发文档请参考 [插件开发](./plugin.md)。 Please refer to the detailed guides of [plugin development](./plugin.md). Read more [examples](https://github.com/nocobase/nocobase/blob/main/examples/index.md) of the Application class.
Application 类的更多示例可参考 [examples](https://github.com/nocobase/nocobase/blob/main/examples/index.md)
## 生命周期 ## Life Cycle
根据不同运行模式Application 有三种生命周期: Depending on the running mode, the Application has three life cycle stages.
### 安装 ### Install
使用 `cli` 中的 `install` 命令调用安装。
一般来说,插件在使用之前若需要在数据库中写入新表或者数据,都需要在安装时执行。在初次使用 Nocobase 时也需要调用安装。
* 调用 `load` 方法,载入已注册的插件。 Use the `install` command in `cli` to invoke the installation. Generally, if needs to write new tables or data to the database before using the plugin, you need to do it during installation. Installation is also required when using NocoBase for the first time.
* 触发 `beforeInstall` 事件。
* 调用 `db.sync` 方法,同步数据库。
* 调用 `pm.install` 方法,执行已注册插件的 `install` 方法。
* 写入 `nocobase` 版本。
* 触发 `afterInstall`
* 调用 `stop` 方法,结束安装。
### 启动 * Call the `load` method to load registered plugins.
使用 `cli` 中的 `start` 命令来启动 Nocobase Web 服务。 * Trigger the `beforeInstall` event.
* Call the `db.sync` method to synchronize database.
* Call the `pm.install` method to execute the `install` methods of registered plugins.
* Write the version of `nocobase`.
* Trigger the `afterInstall` event.
* Call the `stop` method to end installation.
* 调用 `load` 方法,载入已注册的插件。 ### Start
* 调用 `start` 方法
* 触发 `beforeStart`
* 启动端口监听
* 触发 `afterStart`
### 更新 Use the `start` command in `cli` to start NocoBase Web service.
当需要更新 Nocobase 时,可使用 `cli` 中的 `upgrade` 命令。 * Call the `load` method to load registered plugins.
* Call the `start` medthod:
* Trigger the `beforeStart` event
* Start port listening
* Trigger the `afterStart` event
* 调用 `load` 方法,载入已注册的插件。 ### Upgrade
* 触发 `beforeUpgrade`
* 调用 `db.migrator.up` 方法,执行数据库迁移。 Use the `upgrade` command in `cli` to upgrade NocoBase Web service when needed.
* 调用 `db.sync` 方法,同步数据库。
* 调用 `version.update` 方法,更新 `nocobase` 版本。 * Call the `load` method to load registered plugins.
* 触发 `afterUpgrade` * Trigger the `beforeUpgrade` event.
* 调用 `stop` 方法,结束更新。 * Call the `db.migrator.up` method to migrate database.
* Call the `db.sync` method to synchronize database.
* Call the `version.update` method to update the version of `nocobase`.
* Trigger the `afterUpgrade` event.
* Call the `stop` medthod to end upgrade.
## 构造函数 ## 构造函数