服务器发送请求(01):模块http

服务器发送请求

模块:http

示例

/*
    从服务器主动发送请求
    http.request(options[, callback])
 */

const http = require('http');
const path = require('path');
const fs = require('fs');

let options = {
    hostname : 'www.baidu.com',
    port : 80
}

const req = http.request(options,(res)=>{
    let info = '';

    res.on('data',(chuck)=>{
        info += chuck;
    });

    res.on('end',()=>{
        fs.writeFile(path.join(__dirname,'baidu.html'),info,(err)=>{
            console.log('已经获取到百度首页内容');
        });
    });

});

req.end();
posted @ 2020-10-28 09:59  mrtransition  阅读(151)  评论(0)    收藏  举报