Node.jsFrameworkComparison:Expressvs.Koavs.HapiJonathanGlockJonathanworksasafullstackengineerusingJavaScriptsuchasNode,JQuery,Angular,Backbone,andMongodb.HealsoisusingtheMicrosoftstack(ASP.NET,C#,EF,andSQL).1IntroductionExpress.jsisthemostpopularNode.jswebapplicationframeworkusedtoday.ItseemstobethebasedependencyinmostNode.jswebapplications,evensomepopularframeworkslikeSails.jsarebuiltoffofExpress.Howevertherearemoreoptionsavailablethathavethesamesinatra-likefeeltothem.ThenexttwomostpopularframeworksareKoaandHapirespectively.Thisisnotgoingtopersuadeyoutouseoneframeworkovertheother,it'smerelygoingtohelpyougetabetterunderstandingofwhateachframeworkcandoandwhereonemighttrumptheother.2FrameworkbackgroundsAllthreeoftheframeworkswewillbelookingathavealotincommon.AllcancreateaserverwithjustafewlinesofcodeandallmakecreatingaRESTAPIverysimple.Letslookathowtheseframeworksbegan.2.1ExpressTheinitialcommitforExpresswasmadeonJune26,2009byTJHolowaychukand660commitslaterversion0.0.1wasreleasedonJanuary2,2010.ThetwomaincontributorsatthattimewereTJandCiaronJessup.Atthetimeofthefirstreleasetheframeworkwasdescribedasperthereadme.mdongithubInsanelyfast(andsmall)server-sideJavaScriptwebdevelopmentframeworkbuiltonnode.jsandV8JavaScriptengine.Fastforwardalmost5yearsand4,925commits,now4.10.1isthelatestversionmaintainedbyStrongLoopasTJisnowconcentratingintheGo-Langcommunity.2.2KoaTheinitialcommitforKoawasjustoverayearagoonAugust17,2013bynoneotherthanTJHolowaychuk.HedescribesitasExpressivemiddlewarefornode.jsusinggeneratorsviacotomakewritingwebapplicationsandRESTAPIsmoreenjoyabletowrite.Koaisadvertisedashavingasmallfootprintof~400SLOC.Itisnowonversion0.13.0with585commits.2.3HapiTheinitialcommitforHapiwasonAugust5,2011byEranHammeramemberofWalmartLabs.HapiwascreatedbypartsofPostmileandwasoriginallybuiltontopofExpress.LateritwasdevelopedintoitsownframeworkbecauseofwhatErinstate'sinhisblog:hapiwascreatedaroundtheideathatconfigurationisbetterthancode,thatbusinesslogicmustbeisolatedfromthetransportlayer...3,816commitslaterHapiisisonversion7.2.0andisstillmaintainedbyEranHammer.Finallyletslookatsomecommunitystatisticstoseehowpopulartheseframeworksare:MetricExpress.jsKoa.jsHapi.jsGithubStars16,1584,8463,283Contributors1634995Packagesthatdependon:3,82899102StackOverflowQuestions11,41972823CreatingaserverThefirststepforanydeveloperwhenworkingonaNode.jswebapplicationistocreateabasicserver.Soletscreateaserverusingeachframeworktoseetheirsimilaritiesanddifferences.3.1Expressvarexpress=require('express');varapp=express();varserver=app.listen(3000,function(){console.log('Expressislisteningto);});Thisisprobablyprettynaturaltoallnodedevelopers.Werequireexpressandtheninstantiateitbyassigningittothevariableapp.Theninstantiateaservertolistentoaport,port3000.Theapp.listen()isactuallyjustawrapperaroundnode'shttp.createServer().3.2Koavarkoa=require('koa');varapp=koa();varserver=app.listen(3000,function(){console.log('Koaislisteningto);});RightawayyoucanseethatKoaissimilartoExpress.Essentallyyoujustrequiredkoainsteadofexpress.Alsoapp.listen()istheexactsamewrapperfunctionasusedinExpress.3.3HapivarHapi=require('hapi');varserver=newHapi.Server(3000);server.start(function(){console.log('Hapiislisteningto);});Hapiistheuniqueoneofthegroup.Firstlikealways,hapiisrequiredbutinsteadofinstantiatingahapiapp,youcreateanewServerandspecifytheport.InExpressandKoawegetacallbackfunctionbutinHapiwegetanewserverobject.Thenoncewecallserver.start()westarttheserveronport3000whichthenreturnsacallback.HoweverthisisnotlikeKoaandExpress,itisnotawrapperaroundhttp.CreateServer(),itisusingit'sownlogic.4RoutesNowletsdigintooneofthemostimportantfeaturesofaserver,routing.FirstletscreatetheclicheHelloworldapplicationforeachframeworkandthenmoveontosomethingalittlemoreuseful,RESTAPI.4.1Helloworld4.1.1Expressvarexpress=require('express');varapp=express();app.get('/',function(req,res){res.send('Helloworld');});varserver=app.listen(3000,function(){console.log('Expressislisteningto);});Weareusingtheget()methodtocatchtheincomingrequestofGET/andtheninvokeacallbackfunctionthathandlestwoparametersreqandres.Forthisexampleweareonlyutilizingrestoreturnbacktothepageastringusingres.send().Expresshasavarietyofbuiltinmethodsthatareusedtohandletheroutingfunctionality.ThefollowingaresomeofthemorecommonlyusedmethodsthataresupportedbyExpress(butnotallofthemethods):get,post,put,head,delete...4.1.2Koavarkoa=require('koa');varapp=koa();app.use(function*(){this.body='Helloworld';});varserver=app.listen(3000,function(){console.log('Koaislisteningto);});KoaisslightlydifferentthanExpress,itisusingES6generators.Anyfunctionprecededbya*meansthefunctionwillreturnageneratorobject.Basicallythesegeneratorsyieldvaluessynchronouslybutthatisbeyondthescopeofthispost.Withintheapp.use()thegeneratorfunctionsetstheresponsebody.InKoatheContextwhichisequivalenttothethisidentifierisanencapsulationofnode'srequestandresponseobjects