Step By Step Building Your First Node.JS Project - Medium

文章推薦指數: 80 %
投票人數:10人

Step By Step Building Your First Node.JS Project. Node.js is becoming one of the most important tools to have in today's software world. GetunlimitedaccessOpeninappHomeNotificationsListsStoriesWritePublishedinTheStartupStepByStepBuildingYourFirstNode.JSProjectNode.jsisbecomingoneofthemostimportanttoolstohaveintoday'ssoftwareworld.Agiledevelopmentisthemaindevelopmentprocessusedbysoftwarecompaniesnowadays.Youwon’tworkinanysoftwarecompanywithoutworkingwithanagileandscrumdevelopmentprocess,andthatisbecauseitallowsfastadaptiontochange.Thearchitectureusedwiththisstyleofdevelopmentisusuallyamicro-servicearchitecture,whereyoubuildeachmodulecompletelyindependentanddecoupledfromoneanothersothattheyareeasilymaintainedandupdatedwithoutaffectingeachother.Nowinorderforthesemodulestocommunicatewitheachother,theyusewebservicesandmostofthetimeweuseRESTservices,andherewherenodecomeshandy.Inmyopinion,Node.jsisthebestNode.jssaJavaScriptruntimebuiltonChrome’sV8JavaScriptengine.Itisaserver-sidescriptingframeworkasopposedtoAngular.jswhichisaclient-sidescriptingframework,thismeansthatNode.jsisusedforbackenddevelopmentandrunsontheserver,unlikeangular.jswhichusedforfrontenddevelopmentandrunsontheclient(browser).NowthatwehaveanoverviewofNode.jslet’sstartbuildingourfirstproject.TostartwithNodeyouneedtoinstallnpm.npmistheworld’slargestsoftwareregistry.Opensourcedevelopersfromeverycontinentusenpmtoshareandborrowpackages,andmanyorganizationsusenpmtomanageprivatedevelopmentaswell.ToinstallnpmrunthefollowingcommandsudoaptinstallnpmThenyouneedtoinstallnode,youcandothisbygoingtonodeofficialwebsitehttps://nodejs.org/en/Nowtestthatyouinstallednodeandnpmproperlybyrunningthefollowingcommandsnpm-vnode-vNowwewillusenpmtocreateourfirstnodeproject,First,let’screateourprojectdirectory,thenweruntheinitcommandmkdirmyappcdmyappnpminitFollowthecommandlinepromptandyoucanleaveeverythingasthedefault,thenitwillaskyouintheendifeverythinglooksok..clickyes.Yourdirectoryshouldlooksomethinglikethis|_myapp|__package.jsonThepackage.jsonfileconsistsofalltheprojectsettingsandothernpmpackagedependencies.Ifyouopenthepackage.jsonitshouldlooklikethis{"name":"myapp","version":"1.0.0","description":"","main":"index.js","scripts":{"test":"echo\"Error:notestspecified\"&&exit1"},"author":"","license":"ISC"}The“name”elementisthenameofyourpackageandifyouuploadyourpackagetothenpmregistryitwillbeunderthestringthatyousetasthevalueofthiselement,inourcaseit’s“myapp”.The“main”elementspecifiesthefilethatactsasyourentrypointwhenyourunyourproject.Asyoucansee,wedon’thavetheindex.jsfileinourprojectdirectory.Sonowwecreatethatfiletouchindex.jssudochmod777index.jsInordertocreateourfirstRESTresource,wewillusetheexpresspackage.InNode.js,expressisyourmosttrustworthyfriend.ExpressisaminimalandflexibleNode.jswebapplicationframeworkthatprovidesarobustsetoffeaturesforwebandmobileapplications.Ourindex.jsfileshouldlooklikethis.constexpress=require("express")varapp=express()app.get("/",function(request,response){response.send("HelloWorld!")})app.listen(10000,function(){console.log("Startedapplicationonport%d",10000)});Ifyouruntheprojectnowyouwillget“Error:Cannotfindmodule‘express’”,thatisbecauseweneedfirsttoinstalltheexpresspackagetoourprojectandtodothatwewillusethefollowingnpmcommandnpminstall--saveexpressAfterrunningthiscommand,yourpackage.jsonwillnowcontainthe“dependencies”elementthatcontainsallthepackagesthatyourprojectdependson.Thepackage.jsonlookslikethisnow.{"name":"myapp","version":"1.0.0","description":"","main":"index.js","scripts":{"test":"echo\"Error:notestspecified\"&&exit1"},"author":"","license":"ISC","dependencies":{"express":"^4.17.1"}}Nowwecansafelyrunourproject.Itisassimpleasrunningthefollowingcommandnodeindex.jsyoucanalsorunyourprogrambyusingthecommand“npmstart”,youcancustomizethebehaviorofthe“npmstart”byaddingittothescriptselementinyourpackage.jsonfileasfollows{"name":"myapp","version":"1.0.0","description":"","main":"index.js","scripts":{"test":"echo\"Error:notestspecified\"&&exit1","start":"nodeindex.js"},"author":"","license":"ISC","dependencies":{"express":"^4.17.1"}}Whenyoucall“http://localhost:10000/”inyourbrowser,youwillseethe“Helloworld!”message.Noticethattheapplicationisstillrunning,becauseNode.jsrunsasaserveryoudon’tneedTomCatoranyotherservertorunyourapplicationandthatisonetheaspectsthatmakenode.jsagoodchoiceforfastdevelopment.Congratulations!!youhavebuiltyourfirstNode.jsprogram.Next,wewilldiscusshowtobuildamorecomplicatedandwell-structuredproject.MorefromTheStartupGetsmarteratbuildingyourthing.FollowtojoinTheStartup’s+8millionmonthlyreaders&+760Kfollowers.ReadmorefromTheStartupRecommendedfromMediumYoriiisinPrismaMediaTherealpowerofWebpack4SplitChunksPluginVarunJavaScriptanditsUse-CasesDerrickStanfieldKivoi10%Offwithpromocode:SuncoastTen#arcade https://www.suncoastarcade.com/shopWyattMcBaininReactNativeCoachIssue8:ThePatentIssueAnkitBoghraPOLYFILLofyourownAdevintainAdevintaTechBlogIntroducingwebworkerstoimprovesubito.itperformance — part2EgretiaIoinEgretiaEgretiaEngineUpdate!UrvashiinBetterProgrammingPlottingaLineChartWithTooltipsUsingReactandD3.jsAboutHelpTermsPrivacyGettheMediumappGetstartedOelbadrawi16FollowersM.Sc.DataEngineeringandAnalyticsandfreelancedeveloperFollowHelpStatusWritersBlogCareersPrivacyTermsAboutKnowable



請為這篇文章評分?