Hello World program in Node.js
Before creating the actual "Hello world" application in Node.js, see the main parts of the Node.js program. A Node.js program includes the following important sections:
Modules needed : We use require directive to load a module Node.js.
Create Server : A server listens to requests from the client side similar to the Apache HTTP Server.
Read the request and return the response : The server created easily will read the HTTP requests by the client from the browser or console screen to return the response.
Create Node.js application
Step 1: Import the necessary modules
We use the require directive to load the http modules and return the expressions to the http variable as follows:
var http = require ( "http" );
Step 2: Create Server
Next step, we will create http and call http.createServer () method to create a new Server and return a Server Instance and then mount it on port 8081. Transfer it to the request and response parameters. Write the following example paragraph about the "Hello World" program.
http . createServer ( function ( request , response ) { // Gui HTTP header cua request // HTTP Status: 200 : OK // Content Type: text/plain response . writeHead ( 200 , { 'Content-Type' : 'text/plain' }); // Gui phan than cua response, bao gom "Hello World" response . end ( 'Hello Worldn' ); }). listen ( 8081 ); // Man hinh Console se in thong bao console . log ( 'Server dang chay tai http://127.0.0.1:8081/' );
This code is enough for creating an HTTP Server to listen and wait for the local 8081 response.
Step 3: Check Request & Response
Put steps 1 and 2 together in a file with the name main.js and turn on HTTP Server as follows:
var http = require ( "http" ); http . createServer ( function ( request , response ) { // Gui HTTP header cua request // HTTP Status: 200 : OK // Content Type: text/plain response . writeHead ( 200 , { 'Content-Type' : 'text/plain' }); // Gui phan than cua response, bao gom "Hello World" response . end ( 'Hello Worldn' ); }). listen ( 8081 ); // Man hinh Console se in thong bao console . log ( 'Server dang chay tai http://127.0.0.1:8081/' );
Start Server and see the results as follows:
$ node main . js
Check the result. Server is turned on.
Server is running http://127.0.0.1:8081/
Create a request to a Node.js Server
Open http://127.0.0.1:8081/ in any browser and see the results:.
Congratulations on creating the first Node.js application successfully. Follow up on the next chapters to get a deeper understanding of Node.js.
According to Tutorialspoint
Previous lesson: Module in Node.js
Next article: REPL Terminal in Node.js
You should read it
May be interested
- How to uninstall Windows Insider Program Windows 10windows 10 windows insider program is a program registered with those who want to experience before the preview will be upgraded to the full version before it is released.
- How to Install Node.Js on Windowsthis wikihow teaches you how to download and test node.js on a windows computer. open the node.js download site. go to https://nodejs.org/en/ in your computer's web browser.
- Learn about the Program files folder in Windowsprogram files is a folder first found in microsoft windows 95 and included in all later versions of microsoft windows, for storing software programs installed on a computer.
- Download World War Z blockbuster and 2 other games are free on Epic Games Storecurrently, the distribution platform epic games store is currently offering a free program for 3 titles including world war z - third-person shooter, figment - action adventure game and tormentor x punisher - shooter top view.
- What is a sandbox and how does it sandbox a program?many browsers today are designed to automatically run in their own sandbox without user settings.
- Instructions for uninstalling Windows Insider Programthe windows insider program allows you to experience new features early, but it comes with the risk of system errors and instability. if you no longer want to use this test build, exiting the program is necessary to ensure performance and stability of your computer.
- How to Clear NPCscan Cachenpcscan is a program add on created to enhance world of warcraft. you can use the program to scan your character files and alert you to non-player character (npc). you can learn how to clear the npcscan cache, in order to reset npcscan to...
- Microsoft rewarded $ 250,000 in a new bug-finding programmicrosoft finally launched a bug bounty program for security researchers and bug hunters, and then reported bugs on the latest windows operating systems and software.
- How to Run a Program on Command Promptthis wikihow teaches you how to start a program on your windows computer from within the command prompt app. you can only run programs that are installed in windows-created folders (e.g., the desktop), though you can add a program's folder...
- How to Open a File with Another Program Using Open With on Windows Vistado you have a file on your computer which seems to always open in another program that you don't want to use? although the program is on your system, it's just not accessing it for either this instance or for all instances. this article...