Sunteți pe pagina 1din 2

KnowledgeBase

ASP.NET

Resources

CLOUD

CSS

JobBoard

DEVOPS

JoinUs

HTML5

About

JAVASCRIPT

NOSQL

PHP

PYTHON

RUBY

WEB DEV

WEB SERVERS

Search...

WORDPRESS

META WCG

HomeJavaScriptNode.jsAGentleIntroductiontoNode.js

ABOUT JINAL PATEL


JinalPatelisaSoftwareConsultantatKeyholeSoftware.Shehasworkedwith.NET,Java,andJavaScript/SPAtechnologiesandloves
achallenge.Beforeshewasacoder,shewasinbiomedicalengineering.

Regstrateen
Facebook
netegratisalamayor
comunidadonlinedelmundo
ydisfruta!

A Gentle Introduction to Node.js


Postedby:JinalPatel inNode.js March1st,2016

Recentlyatwork,IsoughttofamiliarizemyselfwithNode.js.Naturally,Istarted
searchingthepoolofapplicationsinKeyholesGitHubrepository.Thiswasthefirst
timeIwasusingNode.jsandtheinstructionsintheapplicationreadmefiletorunthe
applicationlocallywasthoroughlydetailed.
However,Iranintosomeverybasicquestionstryingtoruntheapplicationlocally.
Basedonmyexperience,Idraftedthesefundamentalinstructionspresentedinthis
post.IhopetheseinstructionsassisttogetyoustartedwithNode.js.

WhatisNode.js?
Node.jsisanopensource,crossplatformJavaScriptruntimebuiltonGoogleChromes
V8JavaScriptengine.
Basically,itprovidesanotherwaytorunJavaScriptonyourcomputer.Node.jsislightweightandusesaneventdriven,nonblockingI/Omodel.
Node.jsisusedbythousandsofdeveloperstodevelopI/Ointensivewebapplications,singlepageapplications,andotherwebapplications.

InstallingNode.jsonWindows10
Node.jsisveryeasytoinstallintwosteps:
1.DownloadtheWindowsexecutablefilefromhttps://nodejs.org/en/download/.
2.Runtheinstallerandfollowtheinstructionsonthescreen.
Onceinstalled,youwillbeabletolaunchNode.jscommandpromptfromWindowsMenu>Allapps>Node.js>Node.jscommand
prompt.Alternatively,youcanopenacommandprompt,typenode,andpressentertogetaninteractiveshellthatwillenableyoutoexecute
JavaScriptcode.

NEWSLETTER

34993insidersarealreadyenjoying
weeklyupdatesandcomplimentary
whitepapers!

Jointhemnowtogainexclusive
accesstothelatestnewsintheWeb
developersworld,aswellasinsightsabout
HTML5,CSS,JavaScript,WordPressandother
relatedtechnologies.

Emailaddress:
Youremailaddress

Signup

Ifyougetanerror,Irecommendthefollowingtroubleshootingsteps:
1.OpentheSettingfromtheWindowsMenu.Searchforeditsystemenvironmentvariablesandopen.SelectEnvironmentVariables.Under
SystemvariablecheckthevalueforthePathvariable.MakesureitcontainsapathtotheNodeApplication.(C:\ProgramFiles\nodejs\).
2.Ifyouarestillgettingerror,makesureyourestartthecommandwindowafterNode.jsisinstalled.
3.IfthePathvariablewascreatedduringtheinstallationofNode.js,youwillneedtorestartthecomputer.

NowWhat?
Youcanexecutecommandsonthenodecommandprompt.Seebelowforanexample.

JOIN US
With 1,240,600 monthly
uniquevisitorsandover
500 authorsweare
placedamongthetopWeb
resourcesanddevelopment
sitesaround.Constantly
beingonthelookoutfor
partnersweencourage
youtojoinus.SoIfyou
haveablogwithuniqueandinterestingcontentthen
youshouldcheckoutourWCGpartnersprogram.
YoucanalsobeaguestwriterforWebCode
Geeksandhoneyourwritingskills!

Iusedtheconsole.log(HelloWorld)command.Youcanseethemessagedisplayedonconsole.Node.jsprintsareturnvalueofeach
command.Sinceconsole.logdoesnotreturnanything,undefinedisprintedatthebottom.Toexittheinteractiveshell,presskeyctrl+c.
YoucanalsoruncommandsfromaJavaScriptfile.CreateafilehelloWorld.jswithconsole.log(HelloWorld)asthecontentusingNotepad.
TosaveaNotepadfileasaJavaScriptfile,selectFile>SaveAs>selectAllfiles(*.)forSaveastype.Enterthefilenamewith.jsextension.
InNode.jscommandprompt,navigatetothelocationofthefileandenterhelloWorld.js.Node,jswillruntheJavaScriptinthefile.Resultswill
bedisplayedonyourconsole.Pleasenote,thatyoucanomitthe.jsextensionasNode.jsautomaticallyassumesthefiletobein.jsformat.

Printingoutputontheconsoleisnotallthatfun.Letsseehowwecanrunaverybasicwebapplication.First,createafilehelloWorldHttp.js
usingthefollowingcontent:
1
2
3
4
5
6
7

varhttp=require('http');

varserver=http.createServer(function(req,res){
res.writeHead(200);
res.end('HelloWorld');
});
server.listen(8080);

Torunthefile,typenodehelloWorldHttp.jsandpressenter.

Youwillnoticewhenyouruntheserver,theprogramdoesnotexit.Node.jswillonlyexitwhenitiscertaintherewillbenomorepossible
event(s).
Inthistutorial,aHTTPserverisopenthatwillbeservingtotherequestsfromtheclients,generatingevents.
Totesttheserver,openawebbrowserandtypehttp://localhost:8080/intheaddressbarandhitenter.ThewebbrowserwilldisplayHello
World.

Node.jsisverybasicanddoesnotperformvariousfunctionsoutoftheboxbuthasawidesetofbuiltinmodulesthatprovidesdifferent
functions.TheHTTPmodulemakesiteasytocreateabasicHTTPserver.

CAREER OPPORTUNITIES
CSS/HTMLWebDeveloperWayfair
Boston,MA
Apr,24
WebDeveloperJavaScript/CSS/HTML/
MVVMExtronElectronics
Raleigh,NC
Apr,02
Amazon(AWS)WebDeveloperwithJava,HTML,
CSS,andJavaScriptAmazonCorporateLLC
Cambridge,MA
May,05
HTMLEmailDeveloper/FrontEnd
Developer(0077980)Epsilon
MountainView,CA
May,04
JuniorFrontEndDeveloper
(JSP)Noxsolutions.com
LosAngeles,CA
May,06
EmailHTMLandFrontEndDeveloperWalmart
eCommerce
SanBruno,CA
Apr,29
HTMLEmailDeveloper/FrontEnd
DeveloperEpsilon
Sunnyvale,CA
May,04
ApplicationDeveloper(EntryLevelorJunior)Omni
One
Chicago,IL
May,03
JuniorWebDesignerTripleNetTechnologies
Bellevue,WA
Apr,26
WebContentSpecialistTidewaterCommunity
College
Norfolk,VA
May,05
1

...

4375

LetslookattheJavaScriptsyntaxintheexamplefiletocreatetheserver.
Freelance

ThefirstlineisusingcoremoduleHTTPbyrequire(http)andassigningittothevariablehttptousethefunctionsinmodule.Thesecondline
isusingmodulesfunctioncreateServertocreatetheserver.Thesyntaxispassingafunction,hereananonymousfunction,asaparameterin
thecreateServerfunction.Theanonymousfunctionsimplytakestwoparameters,req(request)andres(response).Onanyrequest,the
functionisservingbacktheresponsewithheader200(ok)andthetextHelloWorldinthebody.Thethirdlinedictatestheporttheserver
listensto,theexampleusedport8080.Thusnomatterwhatyouenterontheaddressbarafterlocalhost:8080/xyz,itwillalwaysdisplaythe
helloworldtext.

Keyword...

Fulltime

Location...

Intership

Country...

All

FilterResults
jobsby

Conclusion
AgentleintroductiontoNode.jsprovidesastartingpointtoNode.js.Hopefullyfromthispoint,youwillbeabletobuildontheseinstructions
andreviewtutorialsavailableoutontheweb.
Reference:

AGentleIntroductiontoNode.jsfromourWCGpartnerKeyholeSoftwareattheKeyholeSoftwareblog.

DoyouwanttoknowhowtodevelopyourskillsettobecomeaWeb

Parttime

S-ar putea să vă placă și