collectedfusioncharts

fusioncharts  时间:2021-02-25  阅读:()
NotesonNode.
jsandExpress.
jsCreated08/04/11Updated12/04/11,Updated10/24/12,Updated05/22/13,Updated11/21/13,Updated03/12/14,Updated07/19/14Updated09/27/14,Updated11/08/14,Updated12/08/14,Updated02/21/15,Updated04/22/15,Updated07/25/15Updated08/22/15,Updated11/15/15,Updated02/14/16,Updated04/27/16,Updated06/11/16,Updated08/11/16Updated11/02/16,Updated01/18/17,Updated02/14/17,Updated03/14/17,Updated06/25/17,Updated08/25/17Updated09/28/17IntroductionNode.
js(alsocalledsimplyNode)isasoftwaresystemdesignedforwritinghighly-scalableinternetapplications,notablywebservers.
ProgramsarewritteninJavaScript,usingevent-driven,asynchronousI/Otominimizeoverheadandmaximizescalability.
Node.
jsconsistsofGoogle'sV8JavaScriptengineplusseveralbuilt-inlibraries.
Node.
jswascreatedbyRyanDahlstartingin2009,anditsgrowthissponsoredbyJoyent,hisemployer.
SimilarenvironmentswritteninotherprogramminglanguagesincludeTwistedforPython,PerlObjectEnvironmentforPerl,libeventforCandEventMachineforRuby.
UnlikemostJavaScript,itisnotexecutedinawebbrowser,butisinsteadaformofserver-sideJavaScript.
Node.
jsimplementssomeCommonJSspecifications.
UnlikemostJavaScriptsystems,itprovidesaREPLenvironmentforinteractivetesting.
TheLTSversionis6.
11.
3,whilethelatestversionis8.
6.
0.
Express.
jsisoneofthemorepopularapplicationframeworksusedontopofNode.
js.
Itiscurrentlyonversion4.
15.
4.
Itaddssupportforroutes,templates,errorhandling,configuration,andmore.
ResourcesTheprimaryonlinesiteisathttp://nodejs.
org.
AnarticleinSeptember2017aboutuseofNodeJSatlargecompaneshttps://nodesource.
com/blog/how-massive-companies-use-node-js-at-scaleTherewasaverygoodpresentationfromPayPalonusingNode.
jsinenterpriseapplicationsgivenatCodeCamp2013.
Hereisasummary:AcasestudyinhowPayPalrevitalizeditstechstackbymovingfromJava,JSP,andproprietarysolutionstonode.
jsanddust.
jstemplating.
Developeragilitywasourprimarymotivation,butalongthewaywehadtotackleenterprisecultureandteachpeoplethatJavaScriptisnolongera"toy",butapowerfultooltowield.
LinkToSlidesAndCode"Node.
jsinAction(secondedition)"byMikeCantelon,AlexYoung,MarcHarter,T.
J.
HolowaychukandNathanRajlich.
ManningPress,August2017,362pages.
Listprice$49.
99,Amazonprice$34.
86.
NotyetratedonAmazon.
com.
Updatedtoincludenewmaterialonthirdpartylibraries,developmenttools,etc.
"LearningNode.
js:AHands-OnGuidetoBuildingWebApplicationsinJavaScript(secondedition)"byMarcWandschneider.
Addison-Wesley,December2016,320pages.
Rated4.
8starsonAmazon.
com.
"Node.
jsWebDevelopment(ThirdEdition)byDavidHerron.
PacktPublishing,June2016,376pages.
Listprice$44.
99,Amazonprice$40.
31,usedfrom$38.
66.
Rated4.
2starsonAmazon.
com.
Commentssaid:thisbookisagreatstep-by-stepguidetogetstartedwithNodeJSinnotime.
ItusesalotofdetailedexamplestomakeyouunderstandhowNodeJSworksandhowitcanbeusedtoimplementmodernrequirementslikeusingthirdpartyauthentication(e.
g.
loginwithTwitter),NoSQLdatabases(MongoDBisusedhere),responsivewebdesign,ordeployingwithDocker.
"Node.
jsinPractice"byAlexYoungandMarkHarter.
ManningPress,December2014,424pages.
Listprice$49.
99,Amazonprice$38.
56,usedfrom$35.
22.
Rated5.
0starsonAmazon.
com.
Codingbyexample.
CommentssaidthatthebookdoesventureintoExpress,outsidetheNodecore,butinauseful,limitedfashionthatconcentratesontheuseofNodeandExpresstodevelopRESTAPIs.
ThisisaverycommonusecaseforNodethatneedsmorerealworldcoverageandexpositionofbestpractices.
"WebDevelopmentwithNodeandExpress"byEthanBrown.
O'ReillyPress,July2014,330pages.
Listprice$29.
99,Amazonprice$24.
56.
Rated5starsonAmazon.
com.
ThisisaverycomprehensivebookaboutbothNodeandExpress.
WeincludethisbookunderboththeNodeandExpresssectionsofthisdocument,becauseittakesbothtogether,andcoversthetopicsneededtomakeNode.
jsyourcompletewebapplicationserver.
Topicsincluderequestprocessing,security,persistence,deployment,etc.
"Node.
js,MongoDB,andAngularJSWebDevelopment"byBradDayley.
Addison-WesleyProfessional,June2014,696pages.
Listprice$49.
99,Amazonprice$32.
12,Usedfrom$28.
12.
Rated3.
5starsinAmazon.
com.
AvailableonIEEESafaribooks.
Quitecomprehensive–probablythemostcompletebookofitskind.
Reviewswereverygood,butthereweresomeconcernsabouthowthebookwaslaidout.
"Node.
jstheRightWay:Practical,Server-SideJavaScriptThatScales"byJimR.
Wilson.
PragmaticProgrammersProess,December2013,148pages.
Rated4.
6starsonAmazon.
Listprice$17.
00,Amazonprice$14.
98,usedfrom$13.
56.
Alittleshort,butwell-written.
http://javascriptissexy.
com/learn-node-js-completely-and-with-confidence/http://nodeschool.
io/https://blog.
heroku.
com/archives/2015/11/10/node-habits-2016-hasalistofusefulsuggestions,currenttolate2015.
ConceptsThecoreofNode.
jsistheabilitytorunJavaScriptontheserver.
Thesecond-mostimportantpartofNode.
jsistheecosystemofconventionsfororganizingcomplexJavaScriptresources,calledtheNodePackageManager(npm).
WithinthecoreofNodethereis:Event-drivenprogrammingusingcallbacksAnI/Osubsystemthatsupportsfilesandsystem-leveloperations.
AlargenumberofsupportingpluginsSupportfromanincreasinglylargenumberofhosting/cloudservices(suchasHeroku)WithinNPManditstoolsthereis:Astandardfilefordescribingdependencies,calledpackage.
jsonAloaderanddependencyresolver(muchlikeMavenforJavaprograms)Aneco-systemofregisteredplugins,kepttrackofbyversion.
Node.
jsvs.
Apache:Majorityofcomparisonon-lineisreallyaboutNode.
jsvs.
PHPonApache.
Theprimarydifferencecomesdownto:Nodeisalong-runningprocess,whereasApachespawnsmultiplethreads(oneperrequest),whichstartwithafreshstateeverytime.
Everytimethereisarequest,ApacheopensaPHPthread.
Whentherearetoomanyrequestsatthesametime(i.
e.
,underhighloadconditions),ApachemightopentoomanythreadsandoverloadtheCPU.
WhenandWhennottoUseitWhentochooseNode.
js(basedonhttp://stackoverflow.
com/questions/5062614/how-to-decide-when-to-use-nodejs):IfyourserversidecoderequiresverylittleCPUcycles.
Inotherwordsyouaredoingnon-blockingoperationanddoesnothaveheavyalgorithmorlogicwhichconsumeslotsofCPUcycles.
IfyouarefromJavaScriptbackgroundandcomfortableinwritingSingleThreadedcodejustlikeclientsideJS.
Node.
jsstandsoutespeciallywhendoingrealtimeappcombiningwithsocket.
io.
Node.
jsiswellsuitedforapplicationsthathavealotofconcurrentconnectionsandeachrequestonlyneedsveryfewCPUcycles,becausetheeventloop(withalltheotherclients)isblockedduringexecutionofafunction.
WhenNOTtouseNode.
js:YourserverrequestisdependentonheavyCPUusagesuchassupportingacomplexalgorithmorlogic.
Node.
jsitselfdoesnotutilizeallcoreofunderlyingsystemanditissinglethreadedbydefault,youhavetowritelogicbyyourowntoutilizemulti-coreprocessorandmakeitmulti-threaded(thereisnowaclustermoduleformulti-threading).
ArchitectureInNode,allrequestsareputintheeventloopinawaysimilartohowJavaScripthandleseventlisteners.
Acrossallrequests,thereisonlyasinglethread.
ThisisincontrasttotheapproachinApacheWebServerandPHP.
Exampleofblockingvs.
nonblockingi/o://PHPprint("Hello");sleep(5);print("World");//nodeconsole.
log("Hello");setTimeout(function(){console.
log("World");},5000);Thedifferenceisnotmerelysyntactic(Node.
jsusesacallback)becausetheseexamplesepitomizethedistinctionbetweenblockingandnon-blockingcode.
Inthefirstexample,PHPsleep()blocksthethreadofexecution.
Whiletheprogramissleeping,itisnotdoinganythingelse.
Noderegisterseventsandthenrunsaninfinitelooptopollthekerneltoknowwhethertheseeventsarereadytobedispatched.
Whentheyare,theassociatedfunctioncallbacksarefired,anditmoveson.
Ifnoeventsarepolled,Nodejustkeepsgoinguntilneweventsareready.
HowisNode.
jssogoodatmanagingagreatdealofnetworkconcurrencyForexample,inanormallaptop,asimpleHTTPserverwritteninNodeisabletohandlethousandsofclientspersecond.
Thereasonisthatthereisaneventstackmaintainedwithnode,whichiscollectedacrossallrequests.
Thislinkexplainsthiswell:http://stackoverflow.
com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-jsItstates:Node.
jsisbuiltuponlibuv,across-platformlibrarythatabstractsapis/syscallsforasynchronous(non-blocking)input/outputprovidedbythesupportedOSes(Unix,OSXandWindowsatleast).
Nodetacklestheproblemleveragingjavascript'slanguagefeaturestomakethismodelalittlemoresynchronous-lookingbyinducingtheprogrammertoemployacertainprogrammingstyle.
EveryfunctionthatrequestsIOhasasignaturelikefunction(.
.
.
parameters.
.
.
,callback)andneedstobegivenacallbackthatwillbeinvokedwhentherequestedoperationiscompleted(keepinmindthatmostofthetimeisspentwaitingfortheOStosignalthecompletion-timethatcanbespentdoingotherwork).
Javascript'ssupportforclosuresallowsyoutousevariablesyou'vedefinedintheouter(calling)functioninsidethebodyofthecallback-thisallowstokeepstatebetweendifferentfunctionsthatwillbeinvokedbythenoderuntimeindependently.
Moreover,afterinvokingafunctionspawninganIOoperationthecallingfunctionwillusuallyreturncontroltonode'seventloop.
Thisloopwillinvokethenextcallbackorfunctionthatwasscheduledforexecution(mostlikelybecausethecorrespondingeventwasnotifiedbytheOS)-thisallowstheconcurrentprocessingofmultiplerequests.
Asafinalremark,thephrase"everythingrunsinparallelexceptyourcode"doesadecentjobofcapturingthepointthatnodeallowsyourcodetohandlerequestsfromhundredsofthousandsopensocketwithasinglethreadconcurrentlybymultiplexingandsequencingallyourjslogicinasinglestreamofexecution.
InstallationThisdescribeshowweinstalledNode.
jsandNPMonanEC2instance(instructionsfromtheCoursera"StartupEngineering"course):$sudoapt-getupdate#Installaspecialpackage$sudoapt-getinstall-ypython-software-propertiespythong++make#Addanewrepositoryforapt-gettosearch$sudoadd-apt-repositoryppa:chris-lea/node.
js#Updateapt-get'sknowledgeofwhichpackagesarewhere$sudoapt-getupdate#Nowinstallnodejsandnpm$sudoapt-getinstall-ynodejsUsenpminordertoupgradeNode:$sudonpmcacheclean-f$sudonpminstall-gn$sudonstableBasicExampleThisisfromthe"SmashingNode"book:Createaserver:varhttp=require("http");varserv=http.
createServer(function(req,res){res.
writeHead(200,{"Content-Type":"text/html"});res.
end("SmashingNode!
");});serv.
listen(3000);Torunanode.
jsfile:$nodeXXX.
jsArelatedtoolis"npm",wherenpmisthenamefortheNodePackageManager.
Ifyouwanttoinstallsomethingmanually,youmaydo:$npminstallXXX(modulename)formoredetailsonnpm,seebelow.
UsingtheNodePackageManager(npm)Thishasbeenmovedtoourdocument"NotesonNPMandYarn".
UsingModulesEveryfileinNode.
jsisamodule,althoughmodulesdonotnecessaryhavetobethissimple.
Amoduletypicallyexportssomething,usethe"exports"construct.
TheexportsobjectisaspecialobjectcreatedbytheNodemodulesystemineveryfileyoucreateandisreturnedofthevalueofthefunctionwhichimportsthatfile.
UsingRequireRequire()isafunctionthatiskeytothemoduleloading.
Eachmoduleconsistsofcodewhichisloadedintoacontainingmodule,andhenceavoidpollutingthenamespace.
Aloadedmodulecanalsoexportresults,suchasaconnectionobjectinstance,whichcanbeusedbytherequiringcode.
Theloadingofmodulesiscached,sothatamoduleisnotloadedoverandoveragain.
Hence,ifyouarewritingseveralmodulesthatneedssubmodules(andperhapstheirexports),simplyhaveeachmodulerequirethesubmodules,almostlikeaJavaimportstatement.
Iftherearenoexportsthenthisislikeacompile-timeimport,butsinceJavaScriptisalwaysexecuted,thisislikeaconditionalevaluation.
AModulesoCommonthatitshouldbebuiltinTheAsyncmodule.
Thisenablesyoutocreatechained,waterfallsequencesofexecutingasyncfunctioncalls.
Useasync.
waterfall.
Theresultofeachfunctionispassedtothenextone.
Useasync.
seriestocarryoutseveralfunctions,inwhichtheresultofeachfunctioniscollectedintoamap.
Useasync.
paralleltocarryoutfunctionsthatarenotorderdependent.
Thereisnoresult,butthereisanerrorfunctionwhichwillbecalledifoneofthefunctionsfails.
Finallyuseasync.
autowhichletsyoumixorderedandunorderedfunctionstogetherintoonepowerfulsequenceoffunctions.
Thesync.
autofunctionwillfutureouttherequiredorderofexecution,basedoninformationinthesignaturesofthefunctions.
UsingNodemonJustusenodemoninsteadofnodetorunyourcode,andnowyourprocesswillautomaticallyrestartwhenyourcodechanges.
Toinstall,getnode.
js,thenfromyourterminalrun:npminstall-gnodemonnodemonwrapsyourapplication,soyoucanpassalltheargumentsyouwouldnormallypasstoyourapp:nodemonyournodeappForexample,ifmyapplicationacceptedahostandportasthearguments,Iwouldstartitasso:nodemon.
/server.
jslocalhost8080Anyoutputfromthisscriptisprefixedwithnodemon,otherwisealloutputfromyourapplication,errorsincluded,willbeechoedoutasexpected.
Ifnoscriptisgiven,nodemonwilltestforapackage.
jsonfileandiffound,willrunthefileassociatedwiththemainproperty.
Forinstance,inANG06,thepackage.
jsondeclares"main":"web-server.
js".
Hencewecanjusttype"nodemon"toruntheapplication.
Formoreinformationonnodemon,seethedocumentation.
StreamsinNodeNodecontainsalargestreamlibraryforI/O,whichisorganizedsomewhatsimilarlytothestreamlibraryinJava.
Youcancreatestreams,andpipestreamstootherstreamsanddestinations.
Use`fs.
createReadStream()`topipethegivenfileto`process.
stdout`.
`fs.
createReadStream()`takesafileasanargumentandreturnsareadablestreamthatyoucancall`.
pipe()`on.
Here'sareadablestreamthatpipesitsdatato`process.
stderr`:varfs=require('fs');fs.
createReadStream('data.
txt').
pipe(process.
stderr);Hereisanexampleofthe"through"object,whichtakesaninputstream,hasafunctionwhichtransformstheinput,andsendstheresulttoastream.
varthrough=require('through');rocess.
stdin.
pipe(through(function(buf){this.
queue(buf.
toString().
toUpperCase());})).
pipe(process.
stdout);Youcanusethe`split`moduletosplitinputbynewlines.
Forexample:varsplit=require('split');process.
stdin.
pipe(split()).
pipe(through(function(line){console.
dir(line.
toString());}));Willbufferandsplitchunksonnewlinesbeforeyougetthem.
Forexample,forthe`split.
js`wejustwrotewewillgetseparateeventsforeachlineeventhoughthedataprobablyallarrivesonthesamechunk.
`concat-stream`isawritestreamthatyoucanpassacallbackto,inordertogetthecompletecontentsofastreamasasinglebuffer.
AnexampleofcombiningtheseconceptswouldbetowriteaHTTPwebserver,whichcanhandleanysizeofcontentsinceitusesstreamsforthecontent:varhttp=require('http');varthrough=require('through');varserver=http.
createServer(function(req,res){if(req.
method==='POST'){req.
pipe(through(function(buf){this.
queue(buf.
toString().
toUpperCase());})).
pipe(res);}elseres.
end();});server.
listen(process.
argv[2]);WebsocketStreamsWebsocketstreamsimplementthewebsocketprotocol.
varws=require('websocket-stream');varstream=ws('ws://localhost:8000');stream.
end('hello\n');PromisesinNodeThecoreideabehindpromisesisthatapromiserepresentstheresultofanasynchronousoperation.
Apromiseisinoneofthreedifferentstates:pending-Theinitialstateofapromise.
fulfilled-Thestateofapromiserepresentingasuccessfuloperation.
rejected-Thestateofapromiserepresentingafailedoperation.
Onceapromiseisfulfilledorrejected,itisimmutable(i.
e.
itcanneverchangeagain).
ExampleConsiderthefollowingcode:weneedtohandleerrorsthrownbyJSON.
parsebutwealsoneedtobecarefulnottohandleerrorsthrownbythecallbackfunction.
Bythetimewe'vedoneallofthisourcodeisamessoferrorhandling:functionreadJSON(filename,callback){fs.
readFile(filename,'utf8',function(err,res){if(err)returncallback(err);try{res=JSON.
parse(res);}catch(ex){returncallback(ex);}callback(null,res);});}Despiteallthismessoferrorhandlingcode,wearestillleftwiththeproblemoftheextracallbackparameterhangingaround.
Promiseshelpyounaturallyhandleerrors,andwritecleanercodebynothavingcallbackparameters,andwithoutmodifyingtheunderlyingarchitecture(i.
e.
youcanimplementtheminpureJavaScriptandusethemtowrapexistingasynchronousoperations).
Nowwecanre-writeouroriginalexampleassimply:functionreadJSON(filename){returnreadFile(filename,'utf8').
then(function(res){returnJSON.
parse(res)})}SinceJSON.
parseisjustafunction,wecouldre-writethisas:functionreadJSON(filename){returnreadFile(filename,'utf8').
then(JSON.
parse);}Thisisveryclosetothesimplesynchronousexamplewestartedoutwith.
Putsimply,.
thenisto.
doneas.
mapisto.
forEach.
Toputthatanotherway,use.
thenwheneveryou'regoingtodosomethingwiththeresult(evenifthat'sjustwaitingforittofinish)anduse.
donewheneveryouaren'tplanningondoinganythingwiththeresult.
jQueryThisfeelslikeagoodtimetowarnyouthatwhatjQuerycallsapromiseisinfacttotallydifferenttowhateveryoneelsecallsapromise.
jQuery'spromiseshaveapoorlythoughtoutAPIthatwilllikelyjustconfuseyou.
Fortunately,insteadofusingjQuery'sstrangeversionofapromise,youcanjustconvertittoareallysimplestandardizedpromise:varjQueryPromise=$.
ajax('/data.
json');varrealPromise=Promise.
resolve(jQueryPromise);//nowjustuse`realPromise`howeveryoulike.
SettingupaLocalDevWebServerIt'sbettertousealocalwebserverinsteadofopeninganindex.
htmlfileinthebrowserdirectly,becausewithawebserverourJavaScriptappswillbeabletomakeAJAX/XHRrequests.
Youcantellwhetherit'saserverorafilebylookingattheURLaddressbar.
Iftheaddressstartswithfilethenit'safileandifitstartswithhttpthenit'saserver.
We'llneedthisfeaturelaterinthefutureprojects.
Typically,alocalHTTPwebserverwilllistentoincomingrequestson127.
0.
0.
1orlocalhost.
YoucangetanyoftheopensourcewebserverslikeApache,MAMPor(myfavoritesbecausetheyarewritteninNode.
js)node-staticorhttp-server.
Toinstallnode-staticorhttp-server,youmusthaveNode.
jsandnpminstalled.
SoassumingyouhaveNode.
jsandnpmonyourmachine,runnpmi-gnode-staticornpmi-ghttp-serverinyourTerminal/CommandPrompt.
Then,navigatetothefolderwiththesourcecodeandrunstaticorhttp-server.
TestingThebooksaystousenodeunit,Mocha,andVowsJS.
WeareusingMochaandNightWatch.
MochaMochaisafeature-richJavaScripttestframeworkrunningonNode.
jsandinthebrowser,makingasynchronoustestingsimpleandfun.
Mochatestsrunserially,allowingforflexibleandaccuratereporting,whilemappinguncaughtexceptionstothecorrecttestcases.
HostedonGitHub.
Example:varassert=require('assert');describe('Array',function(){describe('#indexOf()',function(){it('shouldreturn-1whenthevalueisnotpresent',function(){assert.
equal(-1,[1,2,3].
indexOf(4));});});});Thesearetestsofthefunctionsinyourcode,withtheouterlevelofMochaorganizingthem,andtheinnerlevelbeingasetofasserts.
NightWatchNightwatch.
jsisaneasytouseNode.
jsbasedEnd-to-End(E2E)testingsolutionforbrowserbasedappsandwebsites.
ItusesthepowerfulW3CWebDriverAPItoperformcommandsandassertionsonDOMelements.
Example:client.
url('http://www.
google.
com').
waitForElementVisible('body',1000).
assert.
title('Google').
assert.
visible('input[type=text]').
setValue('input[type=text]','rembrandtvanrijn').
waitForElementVisible('button[name=btnG]',1000).
click('button[name=btnG]').
pause(1000).
assert.
containsText('ol#rsoli:first-child','Rembrandt-Wikipedia').
end();Inthiscase,theE2Etestingsystemisspinningupaninstanceofyourapplication,andlookingforHTMLelementsonthescreen.
ApplicationFrameworksForsomelanguagesandplatformsthereareframeworksthatsimplifythejobofcraftinganHTTP-basedapplication.
PerhapsthebestknownisRails(fortheRubylanguage),butcountlessothersincludeDjango,Sinatra,andCake.
MosthavethegoalofsimplifyingandstructuringthedevelopmentofHTMLapplicationsandsolvingsomecommonproblemslikeroutingtheHTTPrequesttothecorrectcontrollercode,servingstaticassets,andrenderingHTMLtemplates.
IntheNodeworldseveralmodulesandframeworksalsosolvesomeoftheseandotherproblems.
Someareseparatemodulesthatyoucangluetogether,andsome,likeExpress.
js,offeranintegratedsolutionthatyoucanrapidlyimplement.
UsingtheConnectFrameworkNodealsoincludessomeframeworksthateasethedevelopmentofanHTTP-basedapplication.
OneofthemostpopulariscalledConnect.
Connectdefinesamodelformiddlewarecomponentsandanenginetorunthem.
Ingeneral,middlewarecomponentsarepiecesthattheprogrammercanstacktogether.
WhenConnectreceivesanHTTPrequest,themiddlewareenginecallseachofthesecomponentsinorder.
Anyofthesecomponentshasaccesstotherequestobject,whichmeansthattheycanparsesomeheadersandbufferandparsetherequestbody.
Theycanalsochangetheresponseheaders,writeapartofthebody,orendtheresponse.
Connectcomesbundledwithaseriesofmiddlewarecomponents,butyoucaneasilybuildyourown.
Examplevarconnect=require('connect');varapp=connect();//setuploggermiddlewareapp.
use(connect.
logger());//actuallyrespondapp.
use(function(req,res){res.
end('HelloWorld!
');});app.
listen(8080);UsingtheExpress.
jsFrameworkExpressisaminimalandflexibleNode.
jswebapplicationframework,providingarobustsetoffeaturesforbuildingsingleandmulti-page,andhybridwebapplications.
WithamyriadofHTTPutilitymethodsandmiddlewaresupportatyourdisposal,creatingarobustuser-friendlyAPIisquickandeasy.
Expressprovidesathinlayeroffeaturesfundamentaltoanywebapplication,withoutobscuringfeaturesthatyouknowandloveinNode.
js.
TobeasflexibleaspossibleExpressmakesnoassumptionsintermsofstructure.
Routesandotherapplication-specificlogicmayliveinasmanyfilesasyouwish,inanydirectorystructureyouprefer.
HoweverExpressincludesanapplicationgeneratorthatsetsupastandarddirectorystructure(showninanexamplebelow).
Availableaswellarethird-partyextensionstoExpresstosimplifysomeofthesepatterns:ResourcefulroutingNamespacedroutingThecurrentversionis4.
14.
0,releasedinmid-2016.
Apriorversionis4.
12.
3,releasedinApril2015.
Apriorversionis4.
11.
2,releasedearly2015.
Thefirstversionofthe4.
10.
xserieswasinOctober2014.
Apriorversionis4.
8.
3,releasedAugust10,2014.
Thefirstversionofthe4.
8.
xserieswasinlateSpring2014.
Apriorversionis4.
0.
x.
Thefirstversionofthe4.
xserieswasinMarch2014.
Startingwiththe4.
0versions,theConnectframeworkisnolongerused,butsimilarfacilitiesareprovidedwithinExpress.
Apriorversionwasthe3.
3.
xseries.
Thefinalreleasewas3.
3.
5inearly2014,andthefirstreleasewas3.
0.
0in2012.
ResourcesThemainsiteisathttp://expressjs.
com"Express.
jsinAction"byEvanHahn.
ManningPress,April2016,256pages.
Listprice$39.
99,Amazonprice$36.
52,usedfrom$25.
73.
Rated4.
3starsonAmazon.
com.
ThisbookisforExpress4.
x.
getexamplesofhiswritingandthoughtsathiswebsite,http://evanhahn.
com/understanding-express/Welearnedalotfromthiswebsite(whichisforExpress4).
"GettingMEANwithMongo,Express,Angular,andNode"bySimonHolmes.
ManningPress,November2015,440pages.
Listprice$44.
99,Amazonprice$37.
33,usedfrom$24.
00.
Rated4.
7starsonAmazon.
com.
Reallycomprehensive"perfectforfillinginthegaps".
"WebDevelopmentwithNodeandExpress"byEthanBrown.
O'ReillyPress,July2014,330pages.
Listprice$29.
99,Amazonprice$24.
56.
Rated5starsonAmazon.
com.
ThisisaverycomprehensivebookaboutbothNodeandExpress.
Ittakesbothtogether,andcoversthetopicsneededtomakeNode.
jsyourcompletewebapplicationserver,replacingApacheorJBoss,forinstance.
Examplehttp://blog.
ijasoneverett.
com/2013/03/a-sample-app-with-node-js-express-and-mongodb-part-1/InstallationInmostcases,youwilluseExpressbyspecifyingitinthedependenciesofaproject(inpackage.
json).
However,ifyoudowanttoinstallitatthecommandline,use:$npminstallexpressThequickestwaytogetstartedwithExpressistoutilizetheexecutableexpress(1)togenerateanapplicationasshownbelow:Installtheexecutable.
Theexecutable'smajorversionwillmatchExpress's:$npminstall-gexpress-generator@4Createtheapp:$express/tmp/foo&&cd/tmp/fooInstalldependencies:$npminstallStarttheserver(typicallyonport3000)$npmstartExampleProgramusingExpress.
jsHereistheexampleapp.
jsfilethatExpress.
jscreates(asof4.
2.
2orlater):varexpress=require('express');varpath=require('path');varfavicon=require('static-favicon');varlogger=require('morgan');varcookieParser=require('cookie-parser');varbodyParser=require('body-parser');varroutes=require('.
/routes/index');varusers=require('.
/routes/users');varapp=express();//viewenginesetupapp.
set('views',path.
join(__dirname,'views'));app.
set('viewengine','jade');app.
use(favicon());app.
use(logger('dev'));app.
use(bodyParser.
json());app.
use(bodyParser.
urlencoded());app.
use(cookieParser());app.
use(express.
static(path.
join(__dirname,'public')));app.
use('/',routes);app.
use('/users',users);///catch404andforwardtoerrorhandlerapp.
use(function(req,res,next){varerr=newError('NotFound');err.
status=404;next(err);});///errorhandlers//developmenterrorhandler//willprintstacktraceif(app.
get('env')==='development'){app.
use(function(err,req,res,next){res.
status(err.
status||500);res.
render('error',{message:err.
message,error:err});});}//productionerrorhandler//nostacktracesleakedtouserapp.
use(function(err,req,res,next){res.
status(err.
status||500);res.
render('error',{message:err.
message,error:{}});});module.
exports=app;varport=Number(process.
env.
PORT||3000);app.
listen(port,function(){console.
log("Listeningon"+port);});Aftertheinitialdependencyhasloaded,youwillfindthatyouareinstantiatinganExpressappusingvarapp=express();Afterthisthecodegoesintoconfiguringsomeapplicationsettings.
FirstitsetstheviewssettingtotherootofthetemplatefilesusedforcomposingHTMLpages.
Thenitsetstheviewenginesettingtojade.
JadeisatemplatinglanguageforHTMLthatdoeswhatyouwouldexpectfromatemplatinglanguage—itallowsyoutorenderHTMLblocks,renderdynamicvalues,performloops,andincludeothertemplates.
YoucantakealookatsomesimpleJadetemplatesthatareinsidetheviewsdirectorythatExpressjustcreated.
Thelaststepistocallapp.
listen(port).
ApparentlythenewExpressgeneratordoesnotaddapp.
listenstatementintheautogeneratedapp.
jsfile(thispotentialbugisdescribedathttp://stackoverflow.
com/questions/23638026/express-node-js-doesnt-work).
Soaddthelastfourstatementsoftheaboveexampleyourself.
HereisatypicalJadetemplate(thiscreatesalistofusers):h1Userspa(href="/users/new")Createnewprofileul-for(varusernameinusers){lia(href="/users/"+encodeURIComponent(username))=users[username].
name-};ScaffoldingExpresshastakenapagefromRubyonRailsandprovidedautilitytogeneratescaffoldingtostartyourExpressproject.
WhiletheExpressscaffoldingutilityisuseful,itcurrentlydoesn'tgeneratetheframeworkthatsomeauthorsrecommendusing.
Inparticular,itdoesn'tprovidesupportfortheHandlebarstemplatinglanguage,anditalsodoesn'tfollowsomeofthenamingconventionscommonlyused.
Whilenotallprojectswillusethescaffoldingutility,youshouldtakealookatitforbackgroundinformation.
ResultingApplicationStructureThescaffoldgeneratorissimplycalled"express".
Hereisthegeneratedstructure:StaticFilesandViewsExpressreliesonamiddlewaretohandlestaticfilesandviews.
Middlewareisaconceptthatwillbecoveredinmoredetailbelow.
Fornow,it'ssufficienttoknowthatmiddlewareprovidesmodularization,makingiteasiertohandlerequests.
Thestaticmiddlewareallowsyoutodesignateoneormoredirectoriesascontainingstaticresourcesthataresimplytobedeliveredtotheclientwithoutanyspecialhandling.
Thisiswhereyouwouldputthingslikeimages,CSSfiles,andclient-sideJavaScriptfiles.
Inyourprojectdirectory,createasubdirectorycalledpublic(wecallitpublicbecauseanythinginthisdirectorywillbeservedtotheclientwithoutquestion).
Then,beforeyoudeclareanyroutes,you'lladdthestaticmiddleware:app.
use(express.
static(__dirname+'/public'));Thestaticmiddlewarehasthesameeffectascreatingarouteforeachstaticfileyouwanttodeliverthatrendersafileandreturnsittotheclient.
Solet'screateanimgsubdirectoryinsidepublic,andputourlogo.
pngfileinthere.
Nowwecansimplyreference/img/logo.
png(note,wedonotspecifypublic;thatdirectoryisinvisibletotheclient),andthestaticmiddlewarewillservethatfile,settingthecontenttypeappropriately.
Nowlet'smodifyourlayoutsothatourlogoappearsoneverypage:{{{body}}}TheelementwasintroducedinHTML5toprovideadditionalsemanticinformationaboutcontentthatappearsatthetopofthepage,suchaslogos,titletext,ornavigation.
TheRequestandResponseObjectsWhenyou'rebuildingawebserverwithExpress,mostofwhatyou'llbedoingstartswitharequestobjectandendswitharesponseobject.
ThesetwoobjectsoriginateinNodeandareextendedbyExpress.
RequestLet'slookatthemostusefulpropertiesandmethodsoftherequestobject(allofthesemethodsareaddedbyExpress,exceptforreq.
headersandreq.
url,whichoriginateinNode):req.
paramsAnarraycontainingthenamedrouteparameters.
req.
param(name)Returnsthenamedrouteparameter,orGETorPOSTparameters.
Oneauthorrecommendsavoidingthismethod(why)req.
queryAnobjectcontainingquerystringparameters(sometimescalledGETparameters)asname/valuepairs.
req.
bodyAnobjectcontainingPOSTparameters.
ItissonamedbecausePOSTparametersarepassedinthebodyoftheREQUEST,notintheURLlikequerystringparametersreq.
routeInformationaboutthecurrentlymatchedroute.
Primarilyusefulforroutedebugging.
req.
cookies/req.
signedCookiesObjectscontainingcontainingcookievaluespassedfromtheclient.
SeeChapter9.
req.
headersTherequestheadersreceivedfromtheclient.
req.
accepts([types])Aconveniencemethodtodeterminewhethertheclientacceptsagiventypeortypes(optionaltypescanbeasingleMIMEtype,suchasapplication/json,acommadelimitedlist,oranarray).
ThismethodisofprimaryinteresttothosewritingpublicAPIs;itisassumedthatbrowserswillalwaysacceptHTMLbydefault.
req.
ipTheIPaddressoftheclient.
req.
pathTherequestpath(withoutprotocol,host,port,orquerystring).
req.
hostAconveniencemethodthatreturnsthehostnamereportedbytheclient.
Thisinformationcanbespoofedandshouldnotbeusedforsecuritypurposes.
req.
xhrAconveniencepropertythatreturnstrueiftherequestoriginatedfromanAJAXcall.
req.
protocolTheprotocolusedinmakingthisrequest(forourpurposes,itwilleitherbehttporhttps).
req.
secureAconveniencepropertythatreturnstrueiftheconnectionissecure.
Equivalenttoreq.
protocol==='https'.
ResponseLetslookatthemostusefulpropertiesandmethodsoftheresponseobject(alloftheseareaddedbyExpress):res.
status(code)SetstheHTTPstatuscode.
Expressdefaultsto200(OK),soyouwillhavetousethismethodtoreturnastatusof404(NotFound)or500(ServerError),oranyotherstatuscodeyouwishtouse.
Forredirects(statuscodes301,302,303,and307),thereisamethodredirect,whichispreferable.
res.
set(name,value)Setsaresponseheader.
Thisisnotsomethingyouwillnormallybedoingmanually.
res.
cookie(name,value,[options]),res.
clearCookie(name,[options])Setsorclearscookiesthatwillbestoredontheclient.
Thisrequiressomemiddlewaresupport.
res.
redirect([status],url)Redirectsthebrowser.
Thedefaultredirectcodeis302(Found).
Ingeneral,youshouldminimizeredirectionunlessyouarepermanentlymovingapage,inwhichcaseyoushouldusethecode301(MovedPermanently).
res.
send(body),res.
send(status,body)Sendsaresponsetotheclient,withanoptionalstatuscode.
Expressdefaultstoacontenttypeoftext/html,soifyouwanttochangeittotext/plain(forexample),you'llhavetocallres.
set('Content-Type','text/plain\')beforecallingres.
send.
Ifbodyisanobjectoranarray,theresponseissentasJSONinstead(withthecontenttypebeingsetappropriately),thoughifyouwanttosendJSON,oneauthorrecommendsdoingsoexplicitlybycallingres.
jsoninstead.
res.
json(json),res.
json(status,json)SendsJSONtotheclientwithanoptionalstatuscode.
res.
jsonp(json),res.
jsonp(status,json)SendsJSONPtotheclientwithanoptionalstatuscode.
res.
type(type)AconveniencemethodtosettheContent-Typeheader.
Essentiallyequivalenttores.
set('Content-Type',type),exceptthatitwillalsoattempttomapfileextensionstoanInternetmediatypeifyouprovideastringwithoutaslashinit.
Forexample,res.
type('txt')willresultinaContent-Typeoftext/plain.
Thereareareaswherethisfunctionalitycouldbeuseful(forexample,automaticallyservingdisparatemultimediafiles),butingeneral,youshouldavoiditinfavorofexplicitlysettingthecorrectInternetmediatype.
res.
format(object)ThismethodallowsyoutosenddifferentcontentdependingontheAcceptrequestheader.
ThisisofprimaryuseinAPIs.
Here'saverysimpleexample:res.
format({'text/plain':'hithere','text/html':'hithere'}).
res.
attachment([filename]),res.
download(path,[filename],[callback])BothofthesemethodssetaresponseheadercalledContent-Dispositiontoattachment;thiswillpromptthebrowsertodownloadthecontentinsteadofdisplayingitinabrowser.
Youmayspecifyfilenameasahinttothebrowser.
Withres.
download,youcanspecifythefiletodownload,whereasres.
attachmentjustsetstheheader;youstillhavetosendcontenttotheclient.
res.
sendFile(path,[options],[callback])Thismethodwillreadafilespecifiedbypathandsenditscontentstotheclient.
Thereshouldbelittleneedforthismethod;it'seasiertousethestaticmiddleware,andputfilesyouwantavailabletotheclientinthepublicdirectory.
However,ifyouwanttohaveadifferentresourceservedfromthesameURLdependingonsomecondition,thismethodcouldcomeinhandy.
SelectingaTemplateEngineforusewithNodeandExpressWhileJadeisthedefaulttemplateenginesuppliedwithExpress,thereareawiderangeofthemavailable.
Thefollowingcomparisonisderivedfrom:http://www.
bearfruit.
org/2014/01/20/node-js-template-showdown-5-options-compared/BuiltintoExpressAllowslogicintemplatesEncourageslogicintemplatesReusetemplatesclientsideAllowsBootstrapIntegrationIDESupportPartialsNotesJadeyesyesnoNoYesWebstorm,sublimewithadd-inYes+LayoutDefaultforExpress.
Lotsofadd-onsavailablevianpm.
Documentationisincompletebutpassable.
Verycleansyntax.
Easilyreadable.
EJSyesyesyesYesYesWebstorm,sublimewithadd-inNoEmbeddedJavaScript.
DefaultforSails.
js.
SyntaxislikeJSPorERB,notascleanasMustache.
JSHTMLyesbutnoyesnoNoAppearstobebuiltinbutisn't.
Insteadneedjshtml-express.
Nopartialssupport.
Missingvariableintemplateisfatalerror.
Websiteisoutdated.
Automatedtestsarefailing.
Difficulttofinddocumentation.
Seekstobefamilarto.
netdevelopers.
Mustacheyes(olderversionofhogan)nonoYesyesWebstormwithplugin,sublimewithpluginYesVerypopularclient-sidetemplatinglibrary,supportedbymanylanguages.
Usinglogic-lesstemplatescanbefrustratingatfirstHandlebarsno,useexpress-handlebarsnonoYesyesWebstormwithplugin,sublimewithpluginYes+LayoutTemplateengineusedbyGhost.
Verypopularclientsidelibrary,supersetofMustache.
Supportsaneasiertounderstandifsyntaxforthosewhowantminimallogic.
Someofourthoughtsandconclusionsfromthis:JadeisbuiltintoExpress.
jsasthedefault,butitusesaverydifferentHTMLandtemplatelanguagethanwhatwehaveusedwithRails,PHP,orJava.
ManyofthesetemplatelanguagesexistedbeforeNodeasclient-sidetools.
InparticularMustacheisverysimilartothetemplateframeworkwithinBackbone.
Sailsisapromisingwebframework(seeourdocument"NotesonSails"),butitsdefaulttemplateengineofEJS(probablyshortforEmbeddedJavaScript,andnamedtomatchERBforEmbeddedRuby)isnotconsideredaseasytouse.
Mustacheisnamedforitsuseof{{and}}astemplatemarkersHandlebarsseemstobeasupersetofMustache,hencethetwoofthemlookedthesameduringourevalsinlate2013.
WeshouldbefocusingonHandlebars,andthenonSailswithHandlebars.
ChangingtheTemplateEnginetoHandlebarsThisisbasedhttp://runnable.
com/U07z_Y_j9rZk1tTx/handlebars-template-examples-with-express-4-for-node-jsvarexpress=require('express');varpath=require('path');varexpressHbs=require('express-handlebars');varapp=express();//configurethetemplateengineandmakeitthecurrentviewrenderingengineapp.
engine('hbs',expressHbs({extname:'hbs',defaultLayout:'main.
hbs'}));app.
set('viewengine','hbs');app.
get('/',function(req,res){res.
render('index');});app.
get('/simple',function(req,res){vardata={name:"Furby",color:"black",nickname:"LooLoo"};res.
render('simple',data);});///catch404andforwardtoerrorhandlerapp.
use(function(req,res,next){varerr=newError('NotFound');err.
status=404;next(err);});module.
exports=app;varport=Number(process.
env.
PORT||5000);app.
listen(port,function(){console.
log("Listeningon"+port);});ThemostimportantlineinthisfileistheonethatconfigurestheHandlebarsengine.
Thentheres.
render()methodswilldispatchtothisengine,findthespecifiedtemplateinthe"views"directory,andsubstitutetheinformationfromthe"data"hashmapintothecontents.
Someexampletemplatefileslooklike:main.
hbs:HandlebarsexampleswithExpress4{{{body}}}simple.
hbs:Example:SimpleAnexampletoyisa:{{name}}Color:{{color}}Nickname:{{nickname}}AddingHelperMethods//Configurethetemplateengineandmakeitthecurrentviewrenderingenginevarhbs=expressHbs.
create({extname:'hbs',defaultLayout:'main.
hbs',helpers:{formatPercentage:function(val){returnMath.
round(100*val);}}});app.
engine('hbs',hbs.
engine);app.
set('viewengine','hbs');Tousethishelperinthetemplate,code{{formatPercentagepercentage}}wherepercentageisafloatbetween0and1.
FormHandlingIfyou'reusingGETforyourformhandling,yourfieldswillbeavailableonthereq.
queryobject.
Forexample,ifyouhaveanHTMLinputfieldwithanameattributeofemail,itsvaluewillbepassedtothehandlerasreq.
query.
email.
There'sreallynotmuchmorethatneedstobesaidaboutthisapproach:it'sjustthatsimple.
Ifyou'reusingPOST(recommended),you'llhavetolinkinmiddlewaretoparsetheURL-encodedbody.
First,installthebody-parsermiddleware(npminstall--savebody-parser),thenlinkitin:app.
use(require('body-parser')());Occasionally,youwillseetheuseofexpress.
bodyParserdiscouraged,andforgoodreason.
However,thisissuewentawaywithExpress4.
0,andthebody-parsermiddlewareissafeandrecommended.
Onceyou'velinkedinbody-parser,you'llfindthatreq.
bodynowbecomesavailableforyou,andthat'swhereallofyourformfieldswillbemadeavailable.
Notethatreq.
bodydoesn'tpreventyoufromusingthequerystring.
Let'sgoaheadandaddaformthatletstheusersignupforamailinglist.
Fordemonstration'ssake,we'llusethequerystring,ahiddenfield,andvisiblefieldsin/views/newsletter.
handlebars:Signupforournewslettertoreceivenewsandspecials!
Name

SugarHosts糖果主机圣诞节促销 美国/香港虚拟主机低至6折

SugarHosts 糖果主机商我们算是比较熟悉的,早年学会建站的时候开始就用的糖果虚拟主机,目前他们家还算是为数不多提供虚拟主机的商家,有提供香港、美国、德国等虚拟主机机房。香港机房CN2速度比较快,美国机房有提供优化线路和普通线路适合外贸业务。德国欧洲机房适合欧洲业务的虚拟主机。糖果主机商一般是不会发布黑五活动的,他们在圣圣诞节促销活动是有的,我们看到糖果主机商发布的圣诞节促销虚拟主机低至6折...

Megalayer美国独立服务器配置及性能速度综合评测

Megalayer 商家在之前也有记录过,商家开始只有提供香港站群服务器和独立服务器,后来也有增加到美国独立服务器,以及前几天也有介绍到有增加香港VPS主机。对于香港服务器之前有过评测(Megalayer香港服务器配置一览及E3-1230 8GB服务器评测记录),这里申请到一台美国独立服务器,所以也准备简单的评测记录。目前市场上我们看到很多商家提供VPS或者云服务器基本上没有什么特别的,但是独立服...

ZJI(月付480元),香港阿里云专线服务器

ZJI是成立于2011年原Wordpress圈知名主机商—维翔主机,2018年9月更名为ZJI,主要提供香港、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册业务。本月商家针对香港阿里云线路独立服务器提供月付立减270-400元优惠码,优惠后香港独立服务器(阿里云专线)E3或者E5 CPU,SSD硬盘,最低每月仅480元起。阿里一型CPU:Intel E5-2630L...

fusioncharts为你推荐
Committeesios11设置xp菏泽市牡丹区实验小学支持ipad支持ipad支持ipad支持ipad步骤iosx-routerx-arcsinx的等价无穷小是什么?联通iphone4联通iphone4怎么样,好不好用?
花生壳免费域名申请 host1plus 国外主机 t楼 gitcafe godaddy优惠券 好看的桌面背景图 彩虹ip 丹弗 商务主机 java虚拟主机 智能骨干网 上海域名 bgp双线 合租空间 福建铁通 免费智能解析 服务器是干什么用的 徐州电信 稳定空间 更多