龚箭

node-http(Request)模块

node-http(Request)模块

  1. http.request

    https.request

    发起https请求

    const https = require('https');
    const options = {
     hostname: 'encrypted.google.com',
     port: 443,
     path: '/',
     method: 'GET'
    };
    const req = https.request(options, (res) => {
     console.log('状态码:', res.statusCode);
     console.log('请求头:', res.headers);
     let chunks = [];
     res.on('data', (d) => {
       chunks.push(d)
     });
     res.on('end',()=>{
       const str = Buffer.concat(chunks).toString();
       console.log(str)
     })
    });
    req.on('error', (e) => {
     console.error(e);
    });
    req.end();  
    

    使用https发起get请求
    `javascript

    const querystring = require(‘querystring’)
    const query = querystring.stringify({key:value,key:value});
    // …
    const options = {

     hostname: 'encrypted.google.com'+query,
     port: 443,
     path: '/',
     method: 'GET'
    

    };
    //…

`

47

文章

12

标签