項目地址:koa-typescript-cmsjavascript
最近學習了慕課網七月老師的《從0到1手把手教你用Node.js+KOA2打造超好用的Web框架》,本身使用TypeScript重構了一個簡單的cms框架,具備路由自動註冊、全局異常處理、參數校驗、JWT鑑權、權限管理等cms基礎功能。java
├── dist // ts編譯後的文件
├── src // 源碼目錄
│ ├── components // 組件
│ │ ├── app // 項目業務代碼
│ │ │ ├── api // api層
│ │ │ ├── service // service層
│ │ │ ├── model // model層
│ │ │ ├── validators // 參數校驗類
│ │ │ ├── lib // interface與enum
│ │ ├── core // 項目核心代碼
│ │ ├── middlewares // 中間件
│ │ ├── config // 全局配置文件
│ │ ├── app.ts // 項目入口文件
├── tests // 單元測試
├── package.json // package.json
├── tsconfig.json // ts配置文件
複製代碼
建立項目文件夾,再控制檯中打開此文件夾,輸入npm init -y
初始化package.json
文件。node
根目錄運行tsc --init
後,自動建立tsconfig.json
文件,完成後,執行npm install typescript ts-node @types/node
命令mysql
tsconfig.json
文件源碼:git
{
"compilerOptions": {
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"importHelpers": true, /* Import emit helpers from 'tslib'. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
"typeRoots": ["./node_modules/@types"], /* List of folders to include type definitions from. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": ["node_modules"],
"include": ["src"]
}
複製代碼
執行npm install koa @types/koa
命令 在根目錄建立src
文件夾,並在文件夾下建立app.ts
文件github
app.ts
文件代碼:web
import Koa from 'koa';
const app = new Koa()
app.listen(3000);
console.log('Server running on port 3000');
複製代碼
執行npm install nodemon
命令
修改package.json
文件中的scripts
字段
添加"start": "nodemon -e ts,tsx --exec ts-node ./src/app.ts"
添加此命令後,nodemon
會監聽src
目錄下文件改變,自動重啓sql
修改後:typescript
"scripts": {
"start": "nodemon -e ts,tsx --exec ts-node ./src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
}
複製代碼
再src
目錄建立config
文件夾,並建立config.ts
文件數據庫
export interface configInterface {
environment?: string; // 環境變量
database: databaseInterface; // 數據庫配置
security: securityInterface; // token生成配置
wx: wxInterface; // 微信小程序參數配置
}
複製代碼
export interface securityInterface {
secretKey?: string; // jwt的secretKey
expiresIn?: number; // jwt的失效時間
}
複製代碼
export interface databaseInterface {
dbName: string; // 數據庫名稱
host: string; // 數據庫地址
port: number; // 數據庫端口
user: string; // 數據庫用戶名
password?: string; // 數據庫密碼
}
複製代碼
export interface wxInterface {
AppID?: string; // AppID
AppSecret?: string; // AppSecret
LoginUrl?: string // 小程序登陸請求地址
}
複製代碼
所有配置代碼
export const config: configInterface = {
environment: "dev",
database: {
dbName: "koacms",
host: "localhost",
port: 3306,
user: "root",
password: ""
},
security: {
secretKey: "5465asd6as5d4as65d46sd",
expiresIn: 60 * 60 * 24 * 30
},
wx: {
AppID: "你的AppID",
AppSecret: "你的AppSecret",
LoginUrl: 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code'
}
};
複製代碼