#基于webpack構建的?Koa2?restful?API?服務器腳手架
這是一個基于?Koa2?的輕量級?RESTful?API?Server?腳手架,支持?ES6,?支持使用TypeScript編寫。
GIT地址:https://github.com/Allenzihan/koa2-mysql-framework.git
此腳手架只安裝了一些配合koa2使用的必要插件,不僅提供RESTful?API實現,同時也集成了對靜態資源的處理,支持跨越,代理轉發請求等基礎功能。基本上您僅需要關注您的業務開發即可。
腳手架可以根據不同的環境配置不同的信息運行價值,支持開發,測試,生產環境的不同參數配置。
?#數據庫選型MySQL
當然你也可以根據需要配置其他的關系型數據庫,可擴展?sequelize.js?作為?PostgreSQL,?MySQL,?MariaDB,?SQLite,?MSSQL?關系型數據庫的?ORM,本框架使用MVC分成模式實現,事例上通過SQL去實現對數據庫的增、刪、查、改操作。
?##?目錄結構說明
```bash
├──?README.md
├──?.babelrc????????????????????#?Babel?配置文件
├──?.gitignore??????????????????#?Git?忽略文件列表
├──?package.json????????????????#?描述文件
├──?process.config.js???????????#?pm2?部署示例文件
├──?bin?????????????????????????#?bin入口目錄
│???└──?www?????????????????????#?啟動文件入口
├──?.vscode?????????????????????#?VS?CODE?調式目錄
│???└──?launch.json?????????????#?調試配置
├──?config??????????????????????#?配置文件
│???├──?db.config.js????????????#?數據庫配置文件
│???├──?logger.config.js????????#?日志配置文件
│???├──?proxy.config.js?????????#?代理配置文件
│???└──?session.config.js???????#?session配置文件
├──?src?????????????????????????#?源代碼目錄,編譯后目標源代碼位于?dist?目錄
│???├──?app.js??????????????????#?入口文件
│???├──?files???????????????????#?存放文件目錄
│???├──?middleware??????????????#?中間件目錄
│???????└──?errorRouteCatch.js??#?示例插件?-??router異常處理
│???├──?utils???????????????????#?工具類目錄
│???├──?controllers?????????????#?控制器
│???????└──?usersController.js??#?示例users控制器
│???├──?models??????????????????#?模型層
│???├──?routes??????????????????#?路由層
│?????????└──?users.js??????????#?示例users路由
│???└──?services????????????????#?服務層
│?????????└──?usersService.js???#?示例users服務層
├──?public??????????????????????#?靜態資源目錄
└──?logs????????????????????????#?日志目錄
```
?
##?開發使用說明
?
```bash
git?clone?https://github.com/Allenzihan/koa2-mysql-framework.git
?
cd?mv?koa2-mysql-framework
npm?install
npm?run?dev
?
訪問:?http://127.0.0.1:3000/home
```
##?開發調試說明
?
支持VSCODE調試?Node.js功能,已經配置好,?啟動VSCODE?IDE?上的Debug按鈕即可調試
##?開發環境
npm?run?dev
?
##?PM2?部署說明
提供了?PM2?部署?RESTful?API?Server?的示例配置,位于“process.config.js”文件中。
?
process.config.js?文件提供了兩套環境的配置,分別是測試環境和生產環境的配置
?
啟動測試環境:
npm?run?uat
?
如果啟動失敗,使用pm2?直接啟動
pm2?start?process.config.js?--only?uat
?
啟動生產環境:
npm?run?prod
?
如果啟動失敗,使用pm2?直接啟動
pm2?start?process.config.js?--only?prod
?
以上使用pm2啟動,需提前安裝好pm2模塊
PM2?配合?Docker?部署說明:?http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
?
###?關于?Token?使用的特別說明(JWT?身份認證)
?
app.use(jwt({?
secret:?publicKey.toString()
}).unless({
path:?[
/^\/users\/login/,
/^\/home/,
/^\/assets/
]?
}))
?
在?path?里面的開頭路徑則不進行身份認證,否則都將進行??鑒權。
?
前端處理方案:
?
```javascript
import?axios?from?'axios'
import?{?getToken?}?from?'./tool'
?
const?DevBaseUrl?=?'http://127.0.0.1:8080'
const?ProdBashUrl?=?'https://xxx.xxx'
?
let?config?=?{
baseURL:?process.env.NODE_ENV?!==?'production'???DevBaseUrl?:?ProdBashUrl?//?配置API接口地址
}
?
let?token?=?getToken()
if?(token)?{
config.headers?=?{?Authorization:?'Bearer?'?+?token?}
}
?
let?request?=?axios.create(config)
?
//?http?request?攔截器
axios.interceptors.request.use(
config?=>?{
if?(window)?{
let?token?=?getToken()
if?(token)?{
//?判斷是否存在token,如果存在的話,則每個http?header都加上token
config.headers.Authorization?=?`Bearer?${token}`
}
}
//?if?(config.method?===?'get')?{
//???config.url?=?config.url?+?'timestamp='?+?Date.now().toString()
//?}
return?config
},
err?=>?{
return?Promise.reject(err)
}
)
?
export?default?request
```
?
`tool.js`文件
?
```javascript
//?寫?cookies
export?let?setCookie?=?function?setCookie(name,?value,?time)?{
if?(time)?{
let?strsec?=?getsec(time)
let?exp?=?new?Date()
exp.setTime(exp.getTime()?+?parseInt(strsec))
document.cookie?=
name?+?'='?+?escape(value)?+?';expires='?+?exp.toGMTString()
}?else?{
document.cookie?=?name?+?'='?+?escape(value)
}
}
?
//?讀?cookies
export?let?getCookie?=?function(name)?{
let?reg?=?new?RegExp('(^|?)'?+?name?+?'=([^;]*)(;|$)')
let?arr?=?document.cookie.match(reg)
return?arr???unescape(arr[2])?:?null
}
?
//?刪?cookies
export?let?delCookie?=?function(name)?{
var?exp?=?new?Date()
exp.setTime(exp.getTime()?-?1)
var?cval?=?getCookie(name)
if?(cval?!=?null)?{
document.cookie?=?name?+?'='?+?cval?+?';expires='?+?exp.toGMTString()
}
}
?
//?獲取Token
export?let?getToken?=?function()?{
if?(window.sessionStorage?&&?window.sessionStorage.Bearer)?{
return?window.sessionStorage.Bearer
}?else?if?(window.localStorage?&&?window.localStorage.Bearer)?{
return?window.localStorage.Bearer
}?else?if?(window.document.cookie)?{
return?getCookie('Bearer')
}
}
?
//?設置Token
export?let?setToken?=?function(token,?rememberTime)?{
if?(window.sessionStorage)?{
window.sessionStorage.Bearer?=?token
}
?
if?((rememberTime?&&?window.localStorage)?||?!window.sessionStorage)?{
window.localStorage.Bearer?=?token
}
?
if?(
window.document.cookie?&&
!window.sessionStorage?&&
!window.localStorage
)?{
if?(rememberTime)?{
setCookie('Bearer',?token,?rememberTime)
}?else?{
setCookie('Bearer',?token)
}
}
}
?
//?刪除Token
export?let?delToken?=?function()?{
if?(window.sessionStorage?&&?window.sessionStorage.Bearer)?{
window.sessionStorage.removeItem('Bearer')
}
?
if?(window.localStorage?&&?window.localStorage.Bearer)?{
window.localStorage.removeItem('Bearer')
}
?
if?(window.document.cookie)?{
delCookie('Bearer')
}
}
```
?
大概原理:
通過某個?API(通常是登錄?API)獲取成功后的?Token,存于本地,然后每次請求的時候在?Header?帶上`Authorization:?"Bearer?"?+?token`,通常情況下無需擔心本地?Token?被破解。
GIT地址:?
https://github.com/Allenzihan/koa2-mysql-framework.git
?