1技术栈egg+node项目在线地址http://47.100.30.67:7001项目git地址https://github.com/jiqingpeng/mars/tree/master/fe/egg-example2quickstartegg官方推荐直接使用脚手架,只需要简单的几条指令(npm>=6.1.0)就可以快速生成工程:mkdiregg-example&&cdegg-examplenpminitegg--type=simplenpmi启动项目:npmrundevopenhttp://localhost:70013项目目录结构app/router.js用于配置URL路由规则,详见Router。app/controller/**用于解析用户输入,处理后返回相应的结果,详见Controller。app/model/**框架内置对象,也可以理解为接口名。详情请见型号。config/config.default.js用于编写配置文件。有关详细信息,请参阅配置。config/plugin.js用于配置需要加载的插件,详见Plugins。4项目详细分析app/router.js'usestrict'下的代码分析;/***@param{Egg.Application}app-eggapplication*/module.exports=app=>{const{router,controller}=app;router.get('/',controller.home.render);//home表接口router.resources('users','/users',controller.users);//users表接口router.resources('info','/info',controller.info);//用户表接口router.resources('test','/test',controller.test);//测试表接口router.resources('phone','/phone',controller.phone);//电话表接口};app/controller/info.js//app/controller/users.jsconstController=require('egg').Controller;//将整数方法函数转换为Int(str){if(typeofstr==='number')返回海峡;如果(!str)返回str;返回parseInt(str,10)||0;}//过滤对象中的空属性functionfilterJson(data){constjson={};for(itemindata){if(data[item]){json[item]=data[item];}}returnjson;}classInfoControllerextendsController{//asyncindex(){constctx=this.ctx;让{抵消,限制,性别,尼克}=ctx.query;constwhere=filterJson({sex,nick});offset=toInt(偏移量);限制=toInt(限制);让总计=空;让结果=空;if(JSON.stringify(where)==='{}'){total=(awaitthis.app.model.Info.findAll({})).length;result=awaitthis.app.model.Info.findAll({offset,limit,include:[{model:this.app.model.Phone}]});}else{total=(awaitthis.app.model.Info.findAll({where:where,})).length;result=awaitthis.app.model.Info.findAll({offset,limit,where:where,include:[{model:this.app.model.Phone}]});}ctx.body={数据:结果,总计,};ctx.status=201;}asyncshow(){constctx=this.ctx;const{id}=this.ctx.params;constInfo=awaitctx.model.Info.findById(id,{include:[{模型:this.app.model.Phone,},],});ctx.body={status:true,res:Info,};ctx.status=201;}asynccreate(){constctx=this.ctx;const{phone_id,nick,sex,head_url}=ctx.request.body;constinfo=awaitctx.model.Info.create({phone_id,nick,sex,head_url});ctx.status=201;ctx.body={state:true,res:info,};}异步更新(){constctx=this.ctx;constid=toInt(ctx.params.id);constresult=awaitctx.model.Info.findById(id);const{nick,sex,head_url}=ctx.request.body;awaitresult.update({nick,sex,head_url});ctx.status=201;ctx.body={state:true,res:result,};}asyncdestroy(id){constctx=this.ctx;等待ctx.delete(id);ctx.status=200;}}module.exports=InfoController;findByID/findAll等方法以及参数limt,offset,where等查询规则的使用是基于promise的Node.jsORM的一个库Sequeliz详见SequelizeDocs中文文档版本index/update/show/create/destroyrestful风格定义接口规则如下图。详情参见https://eggjs.org/zh-cn/basics/router.html。5接口文档电话请求url:http://47.100.30.67:7001/info请求方式:GET参数:参数名是否必填typedescriptionphonenostring电话号码注意httpget请求参数要放在url中除非特殊处理请求url:http://47.100.30.67:7001/info请求方式:POST参数:参数名是否必填类型描述phone为字符串,手机号,pwd为字符串密码请求url:http://47.100.30.67:7001/info请求方式:PUT参数:参数名是否为必填typedescriptionpwdnostring密码请求url:http://47.100.30.67:7001/info请求方式:DELETE参数:参数名是否为必需的类型描述id是int帐户id
