当前位置: 首页 > 后端技术 > Node.js

egg-shell-decorators给你的彩蛋加个壳

时间:2023-04-03 14:46:20 Node.js

Egg.js璺敱瑁呴グ鍣紝璁╀綘鐨勫紑鍙戞洿鏁忔嵎~銆怭S锛氭枃鏈湁鎴戠殑鑱旂郴鏂瑰紡锛屾杩庢妧鏈氦娴亊銆慹gg-shell-decorators鏄竴涓狤gg.js鎻掍欢锛屽畠鎻愪緵浜嗕竴绯诲垪Decorators鏉ヨ楗癈ontroller锛屾垜浠厛鏉ョ湅涓€涓緥瀛愶細//app/controller/userimport{Controller}from'egg';import{Get,Message}from'egg-shell-decorators';exportdefaultclassUserControllerextendsController{@Get('/detail/:id')@Message('sogreat!')publicasyncget({params:{id}}){returnawaitthis.service.user.getById(id)}}涓婇潰鏄竴涓畝鍗曠殑controller渚嬪瓙锛屽鏋滄槸egg.js榛樿鐨勫紑鍙戞柟寮忥紝閭d箞灏辫鍦╮outer鏂囦欢涓槧灏勫搴旂殑璺敱锛屼娇鐢╡gg-shell-decorators锛屼笉鍐嶉渶瑕佸湪璺敱鍣ㄦ枃浠朵腑鍋氫换浣曡矾鐢卞尮閰嶃€傚洜涓篹gg-shell-decorators鐨勮矾鐢辫В鏋愪細鑷姩瑙f瀽璺敱锛屽儚涓婇潰鐨勪緥瀛愶紝浼氳嚜鍔ㄨВ鏋愭垚[get]鍏ㄥ眬鍓嶇紑+/user/detail/:id锛岃繖鏍锋垜浠紑鍙戣€呭氨涓嶇敤鍐嶄篃涓嶇敤鎷呭績鏄犲皠闂浜嗭紝鍦–ontroller涓垜浠彲浠ョ煡閬撴瘡涓嚱鏁板搴旂殑璇锋眰淇℃伅銆傚彲鑳戒綘浼氱枒鎯戜笂闈㈢殑@Message涓轰粈涔堜細鐩存帴杩斿洖鏌ヨ鍒扮殑鏁版嵁銆備笅闈㈢郴缁熺殑缁欏ぇ瀹朵粙缁嶄笅鎻掍欢鐨勪娇鐢細瀹夎$npminstallegg-shell-decorators-S濡傛灉涓嶄娇鐢═ypeScript鑴氭墜鏋讹紝闇€瑕佹墽琛屼互涓嬭剼鏈畨瑁呯浉鍏崇殑Babel鎻掍欢锛?npminstallbabel-娉ㄥ唽babel-plugin-transform-decorators-legacy-DUsage鎵€鏈夋紨绀轰唬鐮侀兘浣跨敤TypeScript//app/router.tsimport{Application}from'egg';import{EggShell}from'egg-shell-decorators';exportdefault(app:Application)=>{EggShell(app,{prefix:'/',quickStart:true});};閰嶇疆鍙傛暟锛歱refix:string//鍏ㄥ眬鍓嶇紑quickStart:boolean//鍚敤QuickStart濡傛灉涓嶄娇鐢═ypeScript鑴氭墜鏋讹紝闇€瑕佸湪鍏ュ彛澶勬敞鍐孊able鎻掍欢浠ユ敮鎸丏ecorator//app.js'usestrict';require('babel-register')({plugins:['transform-decorators-legacy',],});涓撲笟鐨勮矾鐢辫В鏋愭槸egg-shell-decorators鏈€澶х殑鐗圭偣銆傜敤Decorator淇グ鐨勮矾鐢变細鑷姩瑙f瀽鎴愬搴旂殑璺敱锛氭枃浠惰矾寰勶細app/controller/home.ts@Get('/detail/:id')@Post('/')resolveroute:[get]globalprefix+/home+/detail/:id[post]鍏ㄥ眬鍓嶇紑+/home+/here鍏ㄥ眬鍓嶇紑鏄寚浣犲湪EggShell涓厤缃殑鍓嶇紑锛岃矾鐢卞垎鏋愭敮鎸佸绾у垎鏋悀MemberHttp鐩稿叧鉁匞et鉁匬ost鉁匬ut鉁匘elete鉁匬atch鉁呴€夐」鉁呭ご閮煍插ご閮╮涓棿浠剁浉鍏斥渽Before鉁匒fter鉁匓eforeAll鉁匒fterAllSwagger鐩稿叧馃敳鏁鏈熷緟涓嬩釜鐗堟湰Others鉁匬refix鉁匨essage鉁匢gnoreJwt鉁匢gnoreJwtAllPrefix濡傛灉浣犱笉鍠滄璺敱瑙f瀽缁欎綘鐨勮矾寰勶紝閭d箞浣犲彲浠ヨ嚜瀹氫箟瑙f瀽鐨勮矾寰?//app/controller/userimport{Controller}from'egg';import{Get,Message,Prefix}from'egg-shell-decorators';@Prefix('/super2god')exportdefaultclassUserControllerextendsController{@Get('/detail/:id')@Message('sogreat!')publicasyncget({params:{id}}){returnawaitthis.service.user.getById(id)}}璺敱瑙f瀽涓鸿繖鏄細鍏ㄥ眬鍓嶇紑+/super2god/detail/:id鑰屼笉鏄叏灞€鍓嶇紑+/user/detail/:idQuickStart鍦‥ggShell涓厤缃畄uickStart涓簍rue浠ュ惎鐢≦uickStart妯″紡锛屽畠浼氳嚜鍔ㄥ鐞嗗搷搴斾綋锛歩mport{Controller}from'egg';import{Get,Message,Error,StatusError}from'egg-shell-decorators';exportdefaultclassUserControllerextendsController{/**status:200{success:true,message:'Great',data:{id:'123',name:'super2god'},}*/@Get('/:id')@Message('Great')publicasyncget({params:{id}}){returnawaitthis.service.user.getById(id)}/**status:200{success:false,message:'intentional'}*/@Post('/:id')publicpost(){throwError('intentional')}/**status:403{success:false,message:'Insufficientpermissions'}*/@Post('/:id')publicpost(){//榛樿鍊糞tatusError鐨勭浜屼釜鍙傛暟鐨勫€间负500throwStatusError('Insufficientpermissions',403)}}RESTful璁╂垜浠娇鐢╡gg-shell-decorators鏉ュ揩閫熺紪鍐欎竴濂桼ESTful椋庢牸鐨勬帴鍙o紙QuickStart妯″紡锛夛細import{Controller}from'egg';import{Get,Post,Put,Delete}from'egg-shell-decorators';exportdefaultclassSubOrderControllerextendsController{@Get('/:id')publicget({params:{id},query:{keyword}}){return`resufulget:${id},${keyword}`;}@Post('/:id')publicpost({params:{id},request:{body:{keyword}}}){return`resufulpost:${id},${keyword}`;}@Put('/:id')publicput({params:{id},request:{body:{keyword}}}){return`resufulput:${id},${keyword}`;}@Delete('/:id')publicdelete({params:{id},request:{body:{keyword}}}){return`resufuldelete:${id},${keyword}`;}}鐢变簬鍐呯疆鐨勨€嬧€媏gg-shell-decorators灏哻tx瀵硅薄浼犲叆浜咰ontroller鐨勫嚱鏁颁腑锛屾垜浠彲浠ョ洿鎺ラ€氳繃缁撴瀯浣撹幏鍙栬姹傚弬鏁帮紝寰堢編~褰撶劧锛岄櫎浜嗚繖鍥涚甯哥敤鐨勮姹傛柟娉?egg-shell-decorators杩樻彁渚涗簡鍏朵粬甯哥敤鐨勮姹傛柟寮忥紝鍏蜂綋璇峰弬鑰冧笂闈㈢殑Http璇锋眰鏂瑰紡銆侸wtJwt鏄洰鍓嶆祦琛岀殑璁よ瘉鏈哄埗锛屾墍浠gg-shell-decorators鎻愪緵浜嗙浉鍏崇殑Decorators銆傚鏋滀娇鐢╡gg-jwt锛岄粯璁ゆ墍鏈夎矾鐢遍兘闇€瑕佽璇侊紝鏈夋椂鎴戜滑甯屾湜鏌愪簺璺敱涓嶈璇侊紝閭d箞鍙渶瑕佽繖鏍峰仛锛歩mport{Controller}from'egg';import{Get,IgnoreJwt}鏉ヨ嚜鈥滆泲澹宠楗板櫒鈥濓紱瀵煎嚭榛樿绫籋omeController鎵╁睍鎺у埗鍣▄@IgnoreJwt@Get('/')publicasyncindex(){return'hi,egg';鏄笉鏄緢绠€鍗曪紝濡傛灉浣犳鏌ュ拷鐣ユ暣涓狢ontroller涔熷緢绠€鍗曪細import{Controller}from'egg';浠?egg-shell-decorators'瀵煎叆{Get,Post,IgnoreJwtAll};@IgnoreJwtAllexport榛樿绫籋omeController鎵╁睍鎺у埗鍣▄@Get('/')publicasyncget(){return'get';}@Post('/')publicasyncpost(){return'post';}}MiddleWareegg-shell-decorators鎻愪緵浜嗗洓涓腑闂翠欢鐩稿叧鐨勮楗板櫒锛岃浣犳洿鏂逛究鐨勪娇鐢ㄤ腑闂翠欢锛歩mport{Controller}from'egg';import{Get,IgnoreJwtAll,Before,After,BeforeAll,AfterAll}from'egg-shell-decorators';constBefore1=require('egg-shell-decorators/test/middlewares/before-1');constBefore2=require('egg-shell-decorators/test/middlewares/before-2');constBefore3=require('铔嬪3-decorators/test/middlewares/before-3');constBefore4=require('egg-shell-decorators/test/middlewares/before-4');constAfter1=require('egg-shell-decorators/test/middlewares/after-1');constAfter2=require('egg-shell-decorators/test/middlewares/after-2');constAfter3=require('egg-shell-decorators/test/middlewares/after-3');constAfter4=require('egg-shell-decorators/test/middlewares/after-4');@BeforeAll([Before1,Before2])@AfterAll([After1,After2])@IgnoreJwtAllexportdefaultclassHomeControllerextendsController{/**涓棿浠跺墠=>1涓棿浠跺墠=>2涓棿浠跺墠=>3涓棿浠跺墠=>4涓昏涓氬姟...涓棿浠跺悗=>1涓棿浠跺悗=>2涓棿浠跺悗=>3涓棿浠跺悗=>4*/@Before([Before3,Before4])@After([After3,After4])@Get('/')publicasyncindex(){return'hi,egg';}}egg-shell-decorators鐨勭敤娉曞凡缁忎粙缁嶈繃浜嗭紝濡傛灉浣犳槸Noder鎴栬€呭枩娆gg.js鐨勬湅鍙嬪彲浠ュ姞鎴戝井鍗氭妧鏈氦娴佸嚱锛岃鎴戜滑涓€璧峰彉寰楁洿濂斤細