📜  Node.js http.server.listen() 方法

📅  最后修改于: 2022-05-13 01:56:38.592000             🧑  作者: Mango

Node.js http.server.listen() 方法

http.server.listen()是 http 模块中 Server 类的内置应用程序编程接口,用于启动服务器以接受新连接。

句法:

const server.listen(options[, callback])

参数:该方法接受以下两个参数:

  • 选项:可以是端口、主机、路径、积压、独占、可读全部、可写全部、ipv6Only等,具体取决于用户需要。
  • callback:可选参数,是作为参数传递的回调函数。

返回值:此方法只返回一个回调函数。

示例 1:文件名-index.js

Javascript
// Node.js program to demonstrate the  
// server.listen() method
  
// Importing http module 
var http = require('http');
  
// Setting up PORT 
const PORT = process.env.PORT || 3000;
  
// Creating http Server 
var httpServer = http.createServer(
    function (request, response) {
  
    // Getting the reference of the
    // underlying socket object
    // by using socket method
    const value = response.socket;
  
    // Display result
    // by using end() method
    response.end("socket buffersize : " 
        + value.bufferSize, 'utf8', () => {
            console.log("displaying the result...");
  
            // Closing server 
            // by using close() method
            httpServer.close(() => {
                console.log("server is closed")
            })
        });
});
  
// Listening to http Server 
// by using listen() method
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});


Javascript
// Node.js program to demonstrate the  
// server.listen() method
  
// Importing http module 
var http = require('http');
  
// Request and response handler 
const httpHandlers = (request, response) => {
  
    // Getting the reference of the
    // underlying socket object
    // by using socket method
    const value = response.socket;
  
    // Display result by using end() method
    response.end("socket local address : " 
        + value.localAddress, 'utf8', () => {
            console.log("displaying the result...");
  
            // Closing server by using close() method
            httpServer.close(() => {
                console.log("server is closed")
            })
        });
};
  
// Listening to http Server 
// by using listen() method
var httpServer = http.createServer(
    httpHandlers).listen(3000, () => {
    console.log("Server is running at port 3000...");
});


使用以下命令运行index.js文件:

node index.js

输出:

Server is running at port 3000...
displaying the result...
server is closed

现在在浏览器中运行http://localhost:3000/ ,您将在屏幕上看到以下输出:

socket buffersize : 0

示例 2:文件名-index.js

Javascript

// Node.js program to demonstrate the  
// server.listen() method
  
// Importing http module 
var http = require('http');
  
// Request and response handler 
const httpHandlers = (request, response) => {
  
    // Getting the reference of the
    // underlying socket object
    // by using socket method
    const value = response.socket;
  
    // Display result by using end() method
    response.end("socket local address : " 
        + value.localAddress, 'utf8', () => {
            console.log("displaying the result...");
  
            // Closing server by using close() method
            httpServer.close(() => {
                console.log("server is closed")
            })
        });
};
  
// Listening to http Server 
// by using listen() method
var httpServer = http.createServer(
    httpHandlers).listen(3000, () => {
    console.log("Server is running at port 3000...");
});

使用以下命令运行index.js文件:

node index.js

输出:

Server is running at port 3000...
displaying the result...
server is closed

现在在浏览器中运行http://localhost:3000/ ,您将在屏幕上看到以下输出:

socket local address : ::1

参考:https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_server_listen