【笔记】Nodejs发送请求

前言

Nodejs通过http模块发送HTTP请求

引入依赖

1
const http = require("http");

发送请求

1
2
3
4
5
6
7
http.request({
method: "GET",
hostname: "http://127.0.0.1",
port: 8080
}, function (response) {
...
}).end();

发送Get请求

1
2
3
http.get("http://127.0.0.1:8080", function (response) {
...
});

获取响应体

  • 通过读取流获取响应体
1
2
3
response.on("data", function (data) {
console.log(data.toString());
});

完成