Step By Step Building Your First Node.JS Project - Medium
文章推薦指數: 80 %
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
延伸文章資訊
- 1Node.js First Example - javaTpoint
A node.js web application contains the following three parts: ... Follow these steps: ... var htt...
- 2Step By Step Building Your First Node.JS Project - Medium
Step By Step Building Your First Node.JS Project. Node.js is becoming one of the most important t...
- 3Getting Started Guide - Node.js
Once we have installed Node.js, let's build our first web server. Create a file named app.js cont...
- 4Node.js First Application - GeeksforGeeks
Node.js First Application ... Node.js is an open source, cross-platform server environment which ...
- 5Node Js for beginners + First Node Js program
Your first Node.js program. Introduction to Node.js. Node.js is an open-source, cross-platform, b...