Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Node.js Creating Your First Application

Node.js Creating Your First Application

In traditional PHP development, you need an HTTP server such as Apache or Nginx, and you need to configure mod_php or php-cgi to handle PHP scripts in order to generate dynamic content. In other words, PHP relies on an external HTTP server to receive requests and serve web pages.

Node.js itself has a built-in HTTP server module, which means that when developing with Node.js, developers can directly use Node.js's HTTP module to create a server, handle HTTP requests, and generate web pages. Therefore, in Node.js, the logic of a web application and the implementation of the HTTP server are tightly integrated, and developers do not need to rely on external HTTP server software.

Before we create our first Node.js "Hello, World!" application, let's first understand what parts a Node.js application consists of:


Creating a Node.js Application

Step 1: Use the require directive to load and import modules

The syntax format is as follows:


const module = require('module-name');

Here, module-name can be a file path (relative or absolute) or a module name. If it is a module name, Node.js will automatically search for the module in the node_modules directory.

The require directive returns the exports object of the loaded module. You can use this object to access the properties and methods defined in the module. If a module has multiple exports, you can use destructuring assignment to obtain them.

We use require directive to load the http module and assign the instantiated HTTP to the variable http, as shown in the following example:


var http = require("http");

Step 2: Create the Server

Next, we use the http.createServer() method to create a server and use the listen method to bind port 8888. The function receives and responds to data through the request and response parameters.

As shown in the example below, create a file named server.js in the root directory of your project and write the following code:

Example

var

http

=

require

(

'http'

)

;

http.

createServer

(

function

(

request

,

response

)

{

       

// Send HTTP headers

       

// HTTP status value: 200: OK

       

// Content type: text/plain

        response.

writeHead

(

200

,

{

'Content-Type'

:

'text/plain'

}

)

;

       

// Send response data "Hello World"

        response.

end

(

'Hello World\n'

)

;

}

)

.

listen

(

8888

)

;

// The terminal prints the following information

console.

log

(

'Server running at http://127.0.0.1:8888/'

)

;

The code above completes a working HTTP server.

Use node Execute the above code with the following command:


node server.js
Server running at http://127.0.0.1:8888/

Next, open your browser and visit http://127.0.0.1:8888/, and you will see a web page displaying "Hello World".

Analyzing the Node.js HTTP Server:


Gif Example Demonstration

Next, we demonstrate the example operations with a Gif image: