or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
Strapi基本概念及架構(Strapi介紹-中篇)
來源: https://strapi.io/documentation/3.0.0-beta.x/concepts/concepts.html
前篇(快速入門)
中篇(概念架構介紹)
後篇(部署及設定)
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →1. Strapi 檔案結構(僅介紹API, config檔案)
API, config, …
2. Strapi 概略運作流程
以 restaurant 資料為例
Request
(此Request介紹為一般的客戶端請求,非原文檔中的Request)
來自客戶端的請求
發送請求給Route端處理
Route(包含分配方法及policies)
原文連結
取得請求後,依照各API的routes.json檔案去做請求的分配,分配給controller處理
./api/restaurant/config/routes.json
Controller
原文連結
從Route中的 handler 取得的方法來做任務執行(類似redux 的 action),將執行services中的方法
./api/restaurant/controllers/Restaurants.js
find
Service
原文連結
取得來自controller的方法呼叫,執行對應的service方法
./api/restaurant/services/Restaurants.js
find
Response
原文連結(response原始資料)
取得service或是controller的資料
e.g., 取得find的資料(API取得的資料)
find
3. Model 資料模型
原文連結
簡單來說,就是在strapi快速入門中,用新增欄位功能產生的資料模型
而我們可以直接使用指令或是修改檔案的方式,來直接新增或是修改欄位!
在新建一個API之後,strapi會自動產生model檔
如果在該API資料夾中無model檔案,可以透過以下CLI指令新建一個
strapi generate:model restaurant name:string
attributes 格式(type)
有以下格式可以使用
string, text, integer, biginteger, float, decimal, password, date, time, datetime, timestamp, boolean, binary, uuid, enumeration, json, email
validations 驗證
有以下驗證可以使用
required (boolean)
: 是否必須unique (boolean)
: 是否唯一index(boolean)
: 是否加上索引值(僅適用mongoDB)max (integer)
: 最大值(整數)min (integer)
: 最小值(整數)以restaurant為例
路徑-
./api/restaurant/models/Restaurant.settings.json
relations 資料關係
詳細範例以及controller相關使用方法請看這邊
One-way 單向
資料之間只有單向的連結,例如
pet
的owner
連結到某位user
,而user
不會有相應的pet
關係Example:
Path —
./api/pet/models/Pet.settings.json
One-to-one 一對一
資料之間為一對一的連結關係,例如
user
會有一個address
,且address
也會有一個相應的user
欄位Example:
Path -
./api/user/models/User.settings.json
Path -
./api/address/models/Address.settings.json
One-to-many 一對多(多對一)
資料之間為一對多(多對一)的連結關係,例如
user
會有很多個articles
,且article
也會有相應的user(article)
欄位Example:
user
Path -
./api/user/models/User.settings.json
article
Path -
./api/article/models/Article.settings.json
Many-to-many 多對多
資料之間為多對多的連結關係,例如
restaurant
會有很多個categories
,且category
也會有相應的restaurant
欄位Example:
restaurant
Path -
./api/user/models/Restaurant.settings.json
category
Path -
./api/article/models/Category.settings.json
4. 常用CLI(Command Line Interface)指令
strapi new
新建一個strapi專案(常用的為–quickstart,即為快速建立專案)
選項:
strapi develop|dev
在
http://localhost:1337
開啟開發模式的strapi頁面加上
/admin
進入儀表板頁面strapi start
在
http://localhost:1337
開啟產品模式的strapi頁面加上
/admin
進入儀表板頁面strapi build
將目前的strapi專案程式打包建構為產品版本
strapi generate:<api名稱>
可以在指令後面直接加上欄位相關的設定
例如 -
strapi generate:restaurant name:string
會自動建立model, controller …等相關資料夾
strapi install <plugin插件名稱>
將會安裝指定插件
例如 -
strapi install graphql
就會安裝graphQL的插件(plugin)