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:
-
The require Directive: In Node.js, the require directive is used to load and import modules. An imported module can be a built-in module, a third-party module, or a custom module.
-
Creating a Server:A server can listen for client requests, similar to HTTP servers such as Apache and Nginx.
-
Receiving Requests and Responding to Requests A server is easy to create; a client can send an HTTP request using a browser or terminal, and the server returns response data after receiving the request.
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:
const http = require('http');: Import Node.js's built-inhttpmodule.http.createServer((req, res) => { ... });: Create a new HTTP server; the callback function is executed every time there is a request.res.writeHead(200, { 'Content-Type': 'text/plain' });: Set the response status code and content type.res.end('Hello World\n');: End the response and send data.server.listen(PORT, () => { ... });: Listen on the specified port and output a message after the server starts.
Gif Example Demonstration
Next, we demonstrate the example operations with a Gif image: