Node.js Web Module
What is a web server?
A Web server generally refers to a website server, a program residing on a certain type of computer on the Internet. The basic function of a Web server is to provide Web information browsing services. It only needs to support the HTTP protocol, HTML document format, and URL, working together with the client's network browser.
Most web servers support server-side scripting languages (php, python, ruby), etc., and use these scripting languages to fetch data from databases and return the results to the client browser.
Currently, the three most mainstream Web servers are Apache, Nginx, and IIS.
Web application architecture
-
Client - Client, generally referring to the browser; the browser can request data from the server through the HTTP protocol.
-
Server - Server, generally referring to the Web server; it can receive client requests and send response data to the client.
-
Business - Business layer, which processes applications through the Web server, such as interacting with databases, performing logical operations, and calling external programs.
-
Data - Data layer, generally composed of databases.
Creating a Web server with Node
Node.js provides the http module, which is mainly used to build HTTP servers and clients. To use HTTP server or client functionality, you must call the http module, as shown in the following code:
var http = require('http');
The following demonstrates the most basic HTTP server architecture (using port 8080). Create a server.js file with the following code:
Example
var
http
=
require
(
'
http
'
)
;
var
fs
=
require
(
'
fs
'
)
;
var
url
=
require
(
'
url
'
)
;
//
Create a server
http
.
createServer
(
function
(
request
,
response
)
{
//
Parse the request, including the file name
var
pathname
=
url
.
parse
(
request
.
url
)
.
pathname
;
//
Output the requested file name
console
.
log
(
"
Request for
"
pathname
"
received.
"
)
;
//
Read the requested file content from the file system
fs
.
readFile
(
pathname
.
substr
(
1
)
,
function
(
err
,
data
)
{
if
(
err
)
{
console
.
log
(
err
)
;
//
HTTP status code: 404 : NOT FOUND
//
Content Type: text/html
response
.
writeHead
(
404
,
{
'
Content-Type
'
:
'
text/html
'
}
)
;
}
else
{
//
HTTP status code: 200 : OK
//
Content Type: text/html
response
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/html
'
}
)
;
//
Response file content
response
.
write
(
data
.
toString
(
)
)
;
}
//
Send response data
response
.
end
(
)
;
}
)
;
}
)
.
listen
(
8080
)
;
//
The console will output the following information
console
.
log
(
'
Server running at http://127.0.0.1:8080/
'
)
;
Next, we create an index.html file in this directory, with the following code:
The index.html file
<!
DOCTYPE
html<
html
<
head
<
meta
charset
=
"
utf-8
"
<
titleRunoob Tutorial(runoob.com)
title
head
<
body
<
h1My first heading
h1
<
p
My first paragraph.
p
body
html
Execute the server.js file:
$ node server.js
Server running at http://127.0.0.1:8080/
Then we open the following address in the browser:http://127.0.0.1:8080/index.html, displaying as shown below:
The console output information from executing server.js is as follows:
Server running at http://127.0.0.1:8080/
Request for /index.html received. # 客户端请求信息
Creating a Web client with Node
To create a Web client with Node, you need to import the http module. Create a client.js file with the following code:
Example
var
http
=
require
(
'
http
'
)
;
//
Options for the request
var
options
=
{
host
:
'
localhost
'
,
port
:
'
8080
'
,
path
:
'
/index.html
'
}
;
//
The callback function for handling the response
var
callback
=
function
(
response
)
{
//
Continuously update data
var
body
=
'
'
;
response
.
on
(
'
data
'
,
function
(
data
)
{
body
+=
data
;
}
)
;
response
.
on
(
'
end
'
,
function
(
)
{
//
Data reception complete
console
.
log
(
body
)
;
}
)
;
}
//
Send a request to the server
var
req
=
http
.
request
(
options
,
callback
)
;
req
.
end
(
)
;
Open a new terminal, execute the client.js file, and the output is as follows:
$ node client.js
Runoob Tutorial(runoob.com)
我的第一个标题
我的第一个段落。
The console output information from executing server.js is as follows:
Server running at http://127.0.0.1:8080/
Request for /index.html received. # 客户端请求信息