CKEditor編輯器串接圖庫- 來源感謝
WeiJ大大: - 申請:
- 來源感謝
-
https://try-json-server-a.koyeb.app
- 暫時
-
https://play-json-server-auth.herokuapp.com
- 已停止
-
https://gpp2p2-3000.sse.codesandbox.io
- 不穩定,偶爾
502-Bad
- 不穩定,偶爾
- 目前使用的檔案
.
├── data/
│ └── db.json // 資料集合
├── server.js // json-server
└── public/ // 前端網頁畫面
├── index.html // 首頁
├── login.html // 登入頁
├── register.html // 註冊頁
├── me/
│ └── bookmarks.html // 使用者收藏列表
├── admin/ // 【後台】
│ ├── desk.html // 【後台】列表
│ └── posts/
│ ├── create.html // 【後台】新增文章
│ └── editor.html // 【後台】編輯文章
└── helpers/
├── renderUserMenu.js // 前台判斷、顯示導覽列
└── uploadAdpter.js // 【後台】編輯器串接圖庫-
index.html、bookmarks.html 共用登入判斷
-
用瀏覽器資料去
getUser來確認登入狀態- 若無法取得就清空瀏覽器
- 代表
token錯誤或過期了
-
用回傳的
user.role字串判斷是否為管理員- 幫管理員顯示後台連結
- 同時取得全部文章、連動收藏列表
- GET
/api/posts?_embed=bookmarks- 加入收藏
- POST
/api/bookmarks
const data = {
postId
};- 移除收藏
- DELETE
/api/bookmarks/${bookmarkId}- 只取得使用者自己的收藏列表、該篇文章內容
- GET
const url = `/api/users/${userId}/bookmarks?_expand=post`;-
【文章系列】:
/api/posts -
【使用者系列】:
/api/users -
每筆新增資料會有自己的
id,由套件自行產生數字
- 不確定能否改用 uuid() 去蓋掉
- 權限通過後,
JSON-Server-auth會自動回傳accessToken、user.id、user.email的使用者資料- 前端需將
accessToken存入localStorage - 並在發送請求前取出來,經由 API 傳給後端
- 前端需將
{
"accessToken": "...",
"user": {
"email": "example@mail",
"id": 2
}
}server.js已增加自訂判斷- 收到
POST方法後會將前端傳來的accessToken解碼 - 解碼後將
userId存回資料
- 收到
-
VSCode
Live Server
-
終端機
- 指令
-
NPM
- 準備電腦放專案的目錄(Windows 系統盡量都英文),範例路徑:
D:\projects- 打開終端機,路徑指向專案目錄
- (Windows 系統可以直接在檔案總管的網址列輸入
CMD)
- (Windows 系統可以直接在檔案總管的網址列輸入
- 抓下來
git clone https://github.com/Aya-X/example-json-server-auth.git- 安裝套件
npm install- 執行
npm run start- 本機端預設網址
http://localhost:3000/-
PORT:3000可以在server.js調整 -
JSON-Server會自動開啟<public>/index.html作為同網域的頁面
-
【版本】:已合併
JSON-server、JSON-Server-auth -
【初始版本】:commit
fefeb0580b- 由
CodeSandbox的JSON-SERVER-STARTER修改而來
- 由
-
原版網頁移動至
<public>/<example>底下- 原版資料集:
<data>/sample.json
- 原版資料集:
- nodemon
- json-server
- json-server-auth
- jwt-decode
跳過編譯工具,先全部使用
CDN匯入
-
Bootstrap5
- CSS
- JS
-
axios
-
CKEditor-v5