Explore the following Mytour article to learn how to create a Node.js web application.
Learn the process of creating a Node.js application
Node.js Console Example
Assuming the file console_example1.js contains the following content
console.log('Hello JavaTpoint');
Open Node.js command prompt and enter the command below:
node console_example1.js
In this case, the console.log() function displays a message on the console.
Creating a Node.js Application
Before delving into the steps to create a web application 'Hello, World!' using Node.js, let's explore the components of a Node.js application with Mytour.
Fundamentally, a Node.js application comprises crucial components as outlined below:
- Import necessary modules: Utilize the Require Directive to load Node.js modules.
- Create a server: The server will listen for client requests, similar to the Apache HTTP server.
- Read requests and provide responses: The server created in the initial step will read client HTTP requests, whether on a browser or dashboard, and deliver responses.
Creating a Node.js Web Application
Refer to the following steps to build a Node.js web application:
Step 1: Import Necessary Modules
Use the Require Directive to load the http module and store the HTTP headers returned into a variable named 'http' as shown below:
var http = require('http');
Step 2: Create a Server
Use the created http instance and call the http.createServer() method to generate a new server instance. Then, associate the server with port 8081 using the listen method linked to the server instances. Pass a function with parameters request and response.
The code snippet above is sufficient to create an HTTP server to listen and wait for requests on port 8081 locally.
The provided code is enough to create an HTTP server to listen and wait for requests on port 8081 locally.
Step 3: Check Request and Response
Save steps 1 and 2 in a file named main.js and initiate the HTTP server as shown below:
How to Start the Server
To start the server, follow the steps below:
First, click on the Start menu, select All Programs, then find and click on Node.js command prompt as shown below:
At this point, the Command Prompt window will appear on the screen as shown below:
Next, enter the command cd desktop in the command prompt window, and then execute the main.js file to start the server by entering the following command:
node main.js
Note: In this guide, Mytour saved the file 'main.js' on the desktop, so use the command cd desktop. If you saved the file elsewhere, replace desktop with the location where you saved the file.
At this point, the server will begin to boot up.
Create a Request for the Node.js Server
Enter http://127.0.0.1:8081/ into the address bar of any browser on your computer and view the results.
From now on, if you make any changes in the 'main.js' file, you will need to re-run the command:
node main.js
So, you have successfully created your first HTTP server. This application responds to all HTTP requests at port 8081.
In this article, Mytour has just guided you on how to create a Node.js application. If you are still uncertain or don't know how to install Node.js, readers can refer to some other articles available on Mytour. In the upcoming articles, Mytour will continue introducing you to the REPL Terminal in Node.js.
