大家好,我是前端工作者B老师,刚才有个前端小弟问我,B老师怎么了?我说怎么回事?给我发几张截图。看!原来昨天有几个小朋友在问egg.js怎么用。他说他想和我一起试试。我说传统功夫讲究的是力量。就算是体重两百磅的英国大力士也扛不住。动动我的手指。我一说他打耳光,他就站了起来。速度很快,然后上来,左前踢,右鞭踢,左刺拳。学习egg.js(部分内容来自egg.js官网)我使用TypeScript创建egg.jsTypeScriptTypeScript是JavaScript类型的超集,可以编译成纯JavaScript。在使用TypeScript开发Egg之前,你会遇到一些影响开发者体验的问题:Egg最本质的Loader自动加载机制导致TS无法静态分析一些依赖。Config自动合并机制下,修改config.{env}.js中插件提供的配置时,如何进行校验和智能提示?开发期间需要开启一个tsc-w独立进程独立构建代码,带来了临时文件位置的纠结和npm脚本的复杂性。单元测试,覆盖率测试,在线错误堆栈如何指向TS源文件而不是编译后的js文件。环境准备操作系统:支持macOS、Linux、Windows运行环境:建议选择LTS版本,最低要求8.x。快速入门通过框架快速启动:$mkdirshowcase&&cdshowcase$npminitegg--type=ts$npmi$npmrundevdirectoryspecification一些限制:Egg目前没有计划使用TS重写。Egg及其对应的插件会提供对应的index.d.ts文件供开发者使用。TypeScript只是我们通过工具链在某种程度上支持的社区实践之一。最低TypeScript要求:版本2.8。整体目录结构与普通Egg项目相同:typescript代码风格,后缀名为tstypings的目录用于放置d.ts文件(大部分会自动生成)showcase├──app│├──controller││└──home.ts│├──service││└──news.ts│└──router.ts├──config│├──config.default.ts│├──config.local.ts│├──config.prod.ts│└──plugin.ts├──test│└──*/.test.ts├──typings│└──*/.d.ts├──README.md├────包。json├──tsconfig.json└──tslint.json配置(Config)Config有点复杂,因为它需要支持:在Controller和Service端使用配置,需要支持多级提示并自动关联.在Config内部,config.view={}的写法应该也支持hints。在config.{env}.ts中,您可以使用config.default.ts自定义配置提示。import{EggAppConfig,EggAppInfo,PowerPartial}from"egg";exportdefault(appInfo:EggAppInfo)=>{constconfig={}asPowerPartial;//从框架/插件覆盖配置//用于cookie签名密钥,应该更改为您自己的并保持安全config.keys=appInfo.name+"_1606967424562_9661";//在此处添加您的egg配置config.middleware=[];//在此处添加您的特殊配置constbizConfig={sourceUrl:`https://github.com/eggjs/examples/tree/master/${appInfo.name}`,};//连接服务器config.mysql={//数据库配置//单数据源客户端//多数据源客户端clients:{db1:{//hosthost:"localhost",//portport:"3306",//用户名user:"root",//密码password:"root",//数据库database:"egg",},db2:{//主机host:"localhost",//端口port:"3306",//用户名user:"root",//密码password:"root",//数据库database:"hubeiwh",},},//所有数据库配置的默认值default:{},//载入应用程序,默认打开//加载对于应用程序,默认是打开app:true,//loadintoagent,defaultisclose//loadintoagent,defaultvalueis"close"agent:false,};//返回的配置将合并到EggAppConfigreturn{...config,...bizConfig,};};控制器(Controller)//app/controller/home.tsimport{Controller}from"egg";exportdefaultclassHomeControllerextendsController{publicasyncindex(){const{ctx}=this;ctx.body=awaitctx.service.test.sayHi("蛋");}publicasyncuser(){const{ctx}=this;ctx.body=等待ctx.service.test.你好(“储物柜”);/***通过name字段查询egg数据库中的username表,查询用户信息*/publicasyncname(){const{ctx}=this;const{name}=ctx.query;尝试{constuser=awaitctx.service.test.name(name);ctx.body={代码:200,数据:用户,消息:“成功”,};}catch(e){ctx.body={code:500,data:`user:${e}`,message:"error",};}}/***通过entPid字段查询hubeiwh库中的cim_enterprise表,查询用户信息*/publicasyncent(){const{ctx}=this;const{entPid}=ctx.询问;尝试{constres=awaitctx.service.test.entprise(entPid);ctx.body={代码:200,数据:res,消息:“成功”,};}catch(e){ctx.body={code:500,data:`user:${e}`,message:"error",};}}}路由(Router)//app/router.tsimport{Application}from'egg';exportdefault(app:Application)=>{const{controller,router}=app;router.get('/',controller.home.index);router.get('/user',controller.home.user);router.get('/name',controller.home.name);router.get('/ent',controller.home.ent);};服务(Service)//app/service/news.tsimport{Service}from"egg";/***测试服务*/exportdefaultclassTestextendsService{/***sayHitoyou*@paramname-你的名字*/publicasyncsayHi(name:string){return`hi,${name}`;}publicasynchello(name:string){return`你好,${name}`;}/***查询蛋库面username表*@param{string}name用户名*/publicasyncname(name:string){constdata:any=awaitthis.app.mysql//使用db1数据库查询.get("db1").query(`SELECT*FROMUSERNAMEWHEREname='${name}'`);返回{数据};}/***查询hubeiwh库用户名表*@param{number}entPid企业id*/publicasyncentprise(entPid:number){constdata:any=awaitthis.app.mysql//使用db2数据库查询.get("db2").get("cim_enterprise",{enterprisepid:entPid});返回{数据};}}```