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

博鳌云¥799/月,香港110Mbps(含10M CN2)大带宽独立服务器/E3/8G内存/240G/500G SSD或1T HDD

博鳌云是一家以海外互联网基础业务为主的高新技术企业,运营全球高品质数据中心业务。自2008年开始为用户提供服务,距今11年,在国人商家中来说非常老牌。致力于为中国用户提供域名注册(国外接口)、免费虚拟主机、香港虚拟主机、VPS云主机和香港、台湾、马来西亚等地服务器租用服务,各类网络应用解決方案等领域的专业网络数据服务。商家支持支付宝、微信、银行转账等付款方式。目前香港有一款特价独立服务器正在促销,...

DiyVM:50元/月起-双核,2G内存,50G硬盘,香港/日本/洛杉矶机房

DiyVM是一家比较低调的国人主机商,成立于2009年,提供VPS主机和独立服务器租用等产品,其中VPS基于XEN(HVM)架构,数据中心包括香港沙田、美国洛杉矶和日本大阪等,CN2或者直连线路,支持异地备份与自定义镜像,可提供内网IP。本月商家最高提供5折优惠码,优惠后香港沙田CN2线路VPS最低2GB内存套餐每月仅50元起。香港(CN2)VPSCPU:2cores内存:2GB硬盘:50GB/R...

阿里云金秋上云季,云服务器秒杀2C2G5M年付60元起

阿里云(aliyun)在这个月又推出了一个金秋上云季活动,到9月30日前,每天两场秒杀活动,包括轻量应用服务器、云服务器、云数据库、短信包、存储包、CDN流量包等等产品,其中Aliyun轻量云服务器最低60元/年起,还可以99元续费3次!活动针对新用户和没有购买过他们的产品的老用户均可参与,每人限购1件。关于阿里云不用多说了,国内首屈一指的云服务器商家,无论建站还是学习都是相当靠谱的。活动地址:h...

fusioncharts为你推荐
newlyroute之路androidwin10关闭445端口win10怎么关闭445的最新相关信息win10445端口怎么样打开电脑10800端口css下拉菜单html+css下拉菜单怎么制作fusionchartsFusionCharts连接数据库你是怎么解决的,能告诉我吗?谢谢啦google中国地图强大的谷歌地图,为什么中国不用起来360chrome360的chrome浏览器进程有点多哦???win7关闭135端口如何关闭135端口,关闭它有什么影响么?迅雷下载速度迅雷下载快慢和什么有关
asp网站空间 com域名 fc2新域名 厦门域名注册 西安服务器租用 免费域名空间申请 vps优惠码cnyvps 免费申请网站域名 sharktech gomezpeer 天猫双十一抢红包 500m空间 蜗牛魔方 qq云端 中国网通测速 hdd gtt t云 Updog 太原联通测速 更多