Node.js First Example - javaTpoint
文章推薦指數: 80 %
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
延伸文章資訊
- 1Step 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...
- 2Node.js - First Application - Tutorialspoint
Creating Node.js Application · Step 1 - Import Required Module · Step 2 - Create Server · Step 3 ...
- 3Node.js First Application - GeeksforGeeks
Node.js First Application ... Node.js is an open source, cross-platform server environment which ...
- 4Node.js First Example - javaTpoint
A node.js web application contains the following three parts: ... Follow these steps: ... var htt...
- 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...