📜  Node.js http.ClientRequest.method API

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

Node.js http.ClientRequest.method API

http.ClientRequest.method是http模块中ClientRequest类的内置应用程序接口,用于获取客户端请求方法的对象。

句法:

const request.method

参数:API不接受任何参数作为参数。

返回值:该方法返回客户端请求方法的对象。

示例 1:

index.js
// Node.js program to demonstrate the  
// request.method APi
  
// Importing http module
const http = require('http');
  
// Create an HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('okay');
});
  
// Now that server is running
server.listen(3000, '127.0.0.1', () => {
  
  // make a request
  const options = {
    port: 3000,
    host: '127.0.0.1',
    headers: {
      'Connection': 'Upgrade',
      'Upgrade': 'websocket'
    }
  };
  
  // getting client request
  const req = http.request(options);
  
  // getting method
  // by using method api
  const v = req.method;
  
  // display the result
  console.log("method :- " + v)
  
  process.exit(0)
});


输出:

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