// 快速初始化egg項目 // 創建egg目錄,並且打開到egg,可省略 $ mkdir egg-example && cd egg-example // 腳手架生成egg項目 $ npm init egg --type=simple // 安裝報錯 error code ENOLOCAL error Could not install from "Files\nodejs\node_cache\_npx\18476" as it does not contain a package.json file. // 解決方案: “更換node cache 路徑”在項目目錄下面運行: #npm config set cache "C:\Users\登錄windows的用戶名\AppData\Roaming\npm-cache"--global 目錄和“--global”之間沒有空格。 // 安裝項目依賴 $ npm i // 啟動項目: $ npm run dev $ open http://localhost:7001 // 目錄結構 egg-project ├── package.json ├── app.js (可選) // 用於自定義啟動時的初始化工作 ├── agent.js (可選) // 用於自定義啟動時的初始化工作 ├── app | ├── router.js // 用於配置 URL 路由規則 │ ├── controller // 用於解析用戶的輸入,處理后返回相應的結果, │ | └── home.js │ ├── service (可選) // 用於編寫業務邏輯層 │ | └── user.js │ ├── middleware (可選) // 用於編寫中間件 │ | └── response_time.js │ ├── schedule (可選) // 用於定時任務 │ | └── my_task.js │ ├── public (可選) // 用於放置靜態資源 index.html, css, img ..... │ | └── reset.css │ ├── view (可選) // 用於放置模板文件 │ | └── home.tpl │ └── extend (可選) // 用於框架的擴展 │ ├── helper.js (可選) │ ├── request.js (可選) │ ├── response.js (可選) │ ├── context.js (可選) │ ├── application.js (可選) │ └── agent.js (可選) ├── config | ├── plugin.js // 用於配置需要加載的插件 | ├── config.default.js // 用於編寫配置文件 │ ├── config.prod.js | ├── config.test.js (可選) | ├── config.local.js (可選) | └── config.unittest.js (可選) └── test // 單元測試 ├── middleware | └── response_time.test.js └── controller └── home.test.js
