0% found this document useful (0 votes)
8 views3 pages

Create Your First Node.js App

utr

Uploaded by

thakurajay8865
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Create Your First Node.js App

utr

Uploaded by

thakurajay8865
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

11/11/24, 9:56 AM Node.

js - First Application

[Link] - First Application


When learning a new language or a framework, the first application to write is a Hello
World program. Such a program displays the Hello World message. This illustrates the
basic syntax of the language and also serves to test whether the installation of the
language compiler has been correctly done or not. In this chapter, we shall write a Hello
World application in [Link].

Console Application
[Link] has a command-line interface. [Link] runtime also allows you to execute
JavaScript code outside the browser. Hence, any JavaScript code can be run in a command
terminal with [Link] executable.

Save the following single line JavaScript as [Link] file.

[Link]("Hello World");

Open a powershell (or command prompt) terminal in the folder in which [Link] file is
present, and enter the following command −

PS D:\nodejs> node [Link]


Hello World

The Hello World message is displayed in the terminal.

Creating [Link] Application


To create a "Hello, World!" web application using [Link], you need the following three
important components −

Import required modules − We use the require directive to load [Link]


modules.

Create server − A server which will listen to client's requests similar to Apache
HTTP Server.

Read request and return response − The server created in an earlier step will
read the HTTP request made by the client which can be a browser or a console and
return the response.

Step 1 - Import Required Module

[Link] 1/3
11/11/24, 9:56 AM [Link] - First Application

We use the require directive to load the http module and store the returned HTTP instance
into an http variable as follows −

var http = require("http");

Step 2 - Create Server


We use the created http instance and call [Link]() method to create a server
instance and then we bind it at port 3000 using the listen method associated with the
server instance. Pass it a function with parameters request and response.

The createserver() method has the following syntax −

[Link](requestListener);

The requestlistener parameter is a function that executes whenever the server receives a
request from the client. This function processes the incoming request and forms a server
reponse.

The requestlistener function takes request HTTP request and response objects from
[Link] runtime, and returns a ServerResponse object.

listener = function (request, response) {


// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
[Link](200, {'Content-Type': 'text/html'});

// Send the response body as "Hello World"


[Link]('<h2 style="text-align: center;">Hello World</h2>');
};

The above function adds the status code and content-type headers to the ServerResponse,
and Hello World message.

This function is used as a parameter to createserver() method. The server is made to


listen for the incoming request at a particular port (let us assign 3000 as the port).

Step 3 - Testing Request & Response


Write the sample implementation to always return "Hello World". Save the following script
as [Link].

[Link] 2/3
11/11/24, 9:56 AM [Link] - First Application

http = require('node:http');
listener = function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/html
[Link](200, {'Content-Type': 'text/html'});

// Send the response body as "Hello World"


[Link]('<h2 style="text-align: center;">Hello World</h2>');
};

server = [Link](listener);
[Link](3000);

// Console will print the message

[Link]('Server running at [Link]

In the PowerShell terminal, enter the following command.

PS D:\nodejs> node [Link]

Server running at [Link]

The program starts the [Link] server on the localhost, and goes in the listen mode at
port 3000. Now open a browser, and enter [Link] as the URL. The
browser displays the Hello World message as desired.

[Link] 3/3

You might also like