Node.js
The application consists of three parts:
(1) require
Instructions:
Node.js
In , use require
the command to load and import modules. The imported modules can be built-in modules, third-party modules or custom modules.
Grammar format:
const module = require('module-name');
module-name
It can be a file path (relative or absolute path), or a module name. If it is a module name, the moduleNode.js
will be automatically searched fromnode_modules
the directory .
require
The command returns the exported object of the loaded module, through which the properties and methods defined in the module can be accessed. If there are multiple exported objects in the module, they can be obtained by destructuring assignment.
var http = require("http");
As shown above, use require
the directive to load http
the module and HTTP
assign the to the variable http
.
(2) Create a server:
The server can listen to the client's request, similar Apache 、Nginx
to HTTP
the server.
http.createServer(function (request, response) {
}).listen(6060 );
As shown above, use http.createServer()
the method to create the server, createServer
pass an anonymous function to the function, the function receives and responds to data through request, response
the parameter , and uses listen
the method to bind 6060
the port .
(3) Receiving requests and responding to requests:
The server is easy to create, the client can use a browser or terminal to send HTTP
a request , and the server returns response data after receiving the request.
http.createServer(function (request, response) {
}).listen(6060 );
When the callback fires, two parameters are passed in: request
and response
. They are objects that can use their methods to handle the details of HTTP
the request and respond to the request (for example: send something back to the requesting browser).
Now, create a new file D:\demo\node
under server.js
to complete a working HTTP
server , the code is as follows:
// 请求(require)Node.js 自带的 http 模块,并且把它赋值给 http 变量
var http = require('http');
const hostname = '127.0.0.1';
const port = 6060;
// 调用 http 模块提供的函数: createServer 。这个函数会返回一个对象,这个对象有一个叫做 listen 的方法,这个方法有一个数值参数, 指定这个 HTTP 服务器监听的端口号
http.createServer(function (request, response) {
console.log("Request received.");
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
// 当收到请求时,使用 response.writeHead() 函数发送一个HTTP状态200和HTTP头的内容类型(content-type)
response.writeHead(200, {
'Content-Type': 'text/plain'});
// 使用 response.write() 函数在HTTP相应主体中发送文本“response message:"
response.write("response message:");
// 调用 response.end() 发送响应数据 "Hello World" 完成响应
response.end('Hello World!\n');
}).listen(port);
// 终端打印如下信息
console.log(`Server running at http://${
hostname}:${
port}/`);
Run the code with node
the command :
Open the browser and visit http://localhost:6060/, you can see the page effect as follows:
(Note that when the server accesses the web page, the server may output "Request received." twice. This is because most servers will try to read http://localhost:6060/
when http://localhost:6060/favicon.ico
)