# egg 心得 框架部份建議直接看 https://eggjs.github.io/zh/ 清楚明白 幾個egg重點 **1. 目錄分層** ``` * Middleware:中介層,進 controller 之前先進這一層。 * Controller:負責reqest、response * Router:路由,把uri 請求指派給對應路徑的 controller。 * Service:業務邏輯层。 * Application:全或物件,可以取得 config、router、controller、logger、controller、middleware、middleware、server、createAnonymous、curl()。 * Context:用户请求的上下文,用于获取请求信息和设置响应信息。 ``` **2. 使用** * Middleware 編寫: ``` 放置在 app/middleware 目錄 ex:app/middleware/response_time.js response_time 就是你寫的 middleware ``` * Middleware 使用: ``` 在 config/config.default.js module.exports = { // 注意是駝峰格式 middleware: [ 'responseTime' ], }; ``` * controller ``` best practice 1. Controller 仅负责 HTTP 层的相关处理逻辑,不要包含太多业务逻辑。 2. 获取用户通过 HTTP 传递过来的请求参数。 3. 校验、组装参数。 4. 调用 Service 进行业务处理。 5. 必要时处理转换 Service 的返回结果,如渲染模板。 6. 通过 HTTP 将结果响应给用户 ``` > **有一個重點 ctx.body 其實等於 ctx.response.body 不要跟 ctx.request.body 搞混**
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up