// 快速初始化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 // 框架内置基础对象 Application, Context, Request, Response 以及框架扩展的一些对象(Controller, Service, Helper, Config, Logger) // Application 对象 // Application 是全局应用对象,在一个应用中,只会实例化一个 // Controller,Middleware,Helper,Service 中都可以通过 this.app 访问到 Application 对象 // 事件: server: 该事件一个 worker 进程只会触发一次,在 HTTP 服务完成启动后,会将 HTTP server 通过这个事件暴露出来给开发者。 error: 运行时有任何的异常被 onerror 插件捕获后,都会触发 error 事件,将错误对象和关联的上下文(如果有)暴露给开发者,可以进行自定义的日志记录上报等处理。 request 和 response: 应用收到请求和响应请求时,分别会触发 request 和 response 事件,并将当前请求上下文暴露出来,开发者可以监听这两个事件来进行日志记录。 // 获取方式: 在继承于 Controller, Service 基类的实例中,可以通过 this.app 访问到 Application 对象。 // Context 对象 // Context 是一个请求级别的对象,框架会将所有的 Service 挂载到 Context 实例上, 最常见的 Context 实例获取方式是在 Middleware, Controller 以及 Service 中,可以通过 this.ctx 访问到 Context 对象 // Request & Response 对象 // Request 是一个请求级别的对象,继承自 Koa.Request。封装了 Node.js 原生的 HTTP Request 对象,提供了一系列辅助方法获取 HTTP 请求常用参数。 // Response 是一个请求级别的对象,继承自 Koa.Response。封装了 Node.js 原生的 HTTP Response 对象,提供了一系列辅助方法设置 HTTP 响应。 //获取方式 可以在 Context 的实例上获取到当前请求的 Request(ctx.request) 和 Response(ctx.response) 实例。 // app/controller/user.js class UserController extends Controller { async fetch() { const { app, ctx } = this; const id = ctx.request.query.id; ctx.response.body = app.cache.get(id); } } // 如上面例子中的 ctx.request.query.id 和 ctx.query.id 是等价的,ctx.response.body= 和 ctx.body= 是等价的。 // 需要注意的是,获取 POST 的 body 应该使用 ctx.request.body,而不是 ctx.body。 // Controller 对象 // 框架提供了一个 Controller 基类,并推荐所有的 Controller 都继承于该基类实现。这个 Controller 基类有下列属性 ctx - 当前请求的 Context 实例。 app - 应用的 Application 实例。 config - 应用的配置。 service - 应用所有的 service。 logger - 为当前 controller 封装的 logger 对象。 // app/controller/user.js const Controller = require('egg').Controller; class UserController extends Controller { // implement } module.exports = UserController; // Service 对象 // 框架提供了一个 Service 基类,并推荐所有的 Service 都继承于该基类实现。 // Service 基类的属性和 Controller 基类属性一致,访问方式也类似: // app/service/user.js const Service = require('egg').Service; class UserService extends Service { // implement } module.exports = UserService;
node_egg框架项目部署_目录结构
猜你喜欢
转载自www.cnblogs.com/JunLan/p/12535930.html
今日推荐
周排行