Node.js First Example - javaTpoint

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

A node.js web application contains the following three parts: ... Follow these steps: ... var http = require("http"); ... How to start your server: Go to start menu ... ⇧SCROLLTOTOP Home C Java AngularJS Node.js Express.js HTML CSS JavaScript jQuery Bootstrap PHP SQL PL/SQL Quiz Projects InterviewQ Comment Forum Training Node.jsTutorial Node.jsTutorial InstallNode.jsWindows InstallNode.jsLinux Node.jsFirstExample Node.jsConsole Node.jsREPL Node.jsNPM Node.jsCLOptions Node.jsGlobals Node.jsOS Node.jsTimer Node.jsErrors Node.jsDNS Node.jsNet Node.jsCrypto Node.jsTLS/SSL Node.jsDebugger Node.jsProcess Node.jsChildProcess Node.jsBuffers Node.jsStreams Node.jsFileSystem Node.jsPath Node.jsStringDecoder Node.jsQueryString Node.jsZLIB Node.jsAssertion Node.jsV8 Node.jsCallbacks Node.jsEvents Node.jsPunycode Node.jsTTY Node.jsWebModules NestJS Node.jsMySQL MySQLCreateConnection MySQLCreateDatabase MySQLCreateTable MySQLInsertRecord MySQLUpdateRecord MySQLDeleteRecord MySQLSelectRecord MySQLSelectUnique MySQLDropTable Node.jsMongoDB CreateConnection CreateDatabase CreateCollection MongoDBInsert MongoDBSelect MongoDBQuery MongoDBSorting MongoDBRemove Difference Node.jsvsAngularJS Node.jsvsPython Node.jsvsPHP Node.jsvsJava Node.jsMCQ Node.jsMCQ Node.jsExpress Express.jsTutorial10+ InterviewQuestions Node.jsInterview AngularJSInterview JavaScriptInterview jQueryInterview Backbone.jsInterview Ember.jsInterview Neo4jInterview next→ ←prev Node.jsFirstExample Therecanbeconsole-basedandweb-basednode.jsapplications. Node.jsconsole-basedExample File:console_example1.js console.log('HelloJavaTpoint'); OpenNode.jscommandpromptandrunthefollowingcode: nodeconsole_example1.js Here,console.log()functiondisplaysmessageonconsole. Node.jsweb-basedExample Anode.jswebapplicationcontainsthefollowingthreeparts: Importrequiredmodules:The"require"directiveisusedtoloadaNode.jsmodule. Createserver:Youhavetoestablishaserverwhichwilllistentoclient'srequestsimilartoApacheHTTPServer. Readrequestandreturnresponse:ServercreatedinthesecondstepwillreadHTTPrequestmadebyclientwhichcanbeabrowserorconsoleandreturntheresponse. Howtocreatenode.jswebapplications Followthesesteps: Importrequiredmodule:Thefirststepistouse?require?directivetoloadhttpmoduleandstorereturnedHTTPinstanceintohttpvariable.Forexample: varhttp=require("http"); Createserver:Inthesecondstep,youhavetousecreatedhttpinstanceandcallhttp.createServer()methodtocreateserverinstanceandthenbinditatport8081usinglistenmethodassociatedwithserverinstance.Passitafunctionwithrequestandresponseparametersandwritethesampleimplementationtoreturn"HelloWorld".Forexample: http.createServer(function(request,response){ //SendtheHTTPheader //HTTPStatus:200:OK //ContentType:text/plain response.writeHead(200,{'Content-Type':'text/plain'}); //Sendtheresponsebodyas"HelloWorld" response.end('HelloWorld\n'); }).listen(8081); //Consolewillprintthemessage console.log('Serverrunningathttp://127.0.0.1:8081/'); Combinestep1andstep2togetherinafilenamed"main.js". File:main.js varhttp=require("http"); http.createServer(function(request,response){ //SendtheHTTPheader //HTTPStatus:200:OK //ContentType:text/plain response.writeHead(200,{'Content-Type':'text/plain'}); //Sendtheresponsebodyas"HelloWorld" response.end('HelloWorld\n'); }).listen(8081); //Consolewillprintthemessage console.log('Serverrunningathttp://127.0.0.1:8081/'); Howtostartyourserver: GotostartmenuandclickontheNode.jscommandprompt. Nowcommandpromptisopen: Setpath:Herewehavesave"main.js"fileonthedesktop. Sotypecddesktoponthecommandprompt.Afterthatexecutethemain.jstostarttheserverasfollows: nodemain.js Nowserverisstarted. MakearequesttoNode.jsserver: Openhttp://127.0.0.1:8081/inanybrowser.Youwillseethefollowingresult. Now,ifyoumakeanychangesinthe"main.js"file,youneedtoagainrunthe"nodemain.js"command. NextTopicNode.jsConsole ←prev next→ ForVideosJoinOurYoutubeChannel:JoinNow Feedback SendyourFeedbackto[email protected] HelpOthers,PleaseShare LearnLatestTutorials Splunk SPSS Swagger Transact-SQL Tumblr ReactJS Regex ReinforcementLearning RProgramming RxJS ReactNative PythonDesignPatterns PythonPillow PythonTurtle Keras Preparation Aptitude Reasoning VerbalAbility InterviewQuestions CompanyQuestions TrendingTechnologies ArtificialIntelligence AWS Selenium CloudComputing Hadoop ReactJS DataScience Angular7 Blockchain Git MachineLearning DevOps B.Tech/MCA DBMS DataStructures DAA OperatingSystem ComputerNetwork CompilerDesign ComputerOrganization DiscreteMathematics EthicalHacking ComputerGraphics SoftwareEngineering WebTechnology CyberSecurity Automata CProgramming C++ Java .Net Python Programs ControlSystem DataMining DataWarehouse JavatpointServicesJavaTpointofferstoomanyhighqualityservices.Mailuson[email protected],togetmoreinformationaboutgivenservices.WebsiteDesigningWebsiteDevelopmentJavaDevelopmentPHPDevelopmentWordPressGraphicDesigningLogoDigitalMarketingOnPageandOffPageSEOPPCContentDevelopmentCorporateTrainingClassroomandOnlineTrainingDataEntryTrainingForCollegeCampusJavaTpointofferscollegecampustrainingonCoreJava,AdvanceJava,.Net,Android,Hadoop,PHP,WebTechnologyandPython.Pleasemailyourrequirementat[email protected]Duration:1weekto2weekLike/Subscribeusforlatestupdatesornewsletter



請為這篇文章評分?