In previous node.js chapters, we learned what is node.js? and how to setup node.js development environment. Now, we will learn how to create simple node.js hello world application in different ways using node.js command-line interface.
In node.js, we can create either console or web applications based on our requirements. Here, we will learn both ways to create node.js simple hello world application with examples.
By using node.js command-line interface, we can create console applications. To open a node.js command-line interface, search for node.js in windows and click on Node.js desktop app like as shown below.
Generally, when we install a node.js in our machine, we will get a Node.js command-line interface and other required components.
After opening Node.js command-line interface, type console.log(“Hello World”)
command in the console and click on the enter button that will show the output like as shown below.
If you observe the above result, we tried to print a message in node.js shell by typing the message “Hello World”. After we type a message and click on Enter, it returned the string whatever we mentioned in a terminal.
The following are the three main components required in node.js to create a web application.
Component | Description |
---|---|
Import Modules | By using require directive we can import required modules in the application. |
Create Server | We need to create a server to listen a client requests similar to Apache or IIS server. |
Request & Response | By using these we can read the client requests and we can return our application response. |
Now create a new folder wherever you want in your system, and in that create a file called Helloworld.js and write the code like as shown below.
// Importing http core package/module provided by nodejs
var http = require('http');
// Creating a server
http.createServer(function (req, res) {
// Write response as Html(text)
res.writeHead(200, {'Content-Type': 'text/html'});
// Writing static text
res.end('Hello World');
// Server listening on port number 4200
}).listen(4200);
If you observe above code, we are importing http
module by using require
directive and created the server to listen a request and response on port number 4200.
Here, the above code will tell the computer to write “Hello World” if anyone tries to access your computer on port 4200.
As of now, you don’t need to worry about the above code, we will learn more about it in the next chapters.
The node.js files can be executed by using a command-line interface program of our computer so open a command prompt (cmd) and navigate to the folder that contains a Helloworld.js file like as shown below.
Generally, the node.js files can be initiated by entering a command like node <Your_File_Name>
so enter a command node Helloworld.js
and hit enter button like as shown below.
Now your computer will work as a server so if anyone tries to access your computer with port 4200, then in return, they will get a “Hello world” message. To test this, open a browser in your machine and type a URL called http://localhost:4200 and click enter, which will show the result as shown below.
This is how we can create a console or web application in node.js based on our requirements. In the next chapters, we will learn more about other topics in node.js with examples.