Javascript mocha超时中的简单GET请求
我有以下代码Javascript mocha超时中的简单GET请求,javascript,node.js,rest,https,mocha.js,Javascript,Node.js,Rest,Https,Mocha.js,我有以下代码 const https = require("https"); it("wait for some result", function (done) { this.timeout(15000); const options = { hostname: "httpbin.org", path: "/get", headers: { Authorization:
const https = require("https");
it("wait for some result", function (done) {
this.timeout(15000);
const options = {
hostname: "httpbin.org",
path: "/get",
headers: {
Authorization: "bearer ",
},
};
https.get(options, (resp) => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", (chunk) => {
data += chunk;
console.log(data);
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
console.log(JSON.parse(data).explanation);
});
resp.on("error", (err) => {
console.log("Error: " + err.message);
});
done();
});
});
这将返回:
.end()
const req=https.get(选项,(resp)=>{
让数据=”;
//已接收到一块数据。
分别在(“数据”、(块)=>{
数据+=块;
控制台日志(数据);
});
//已收到全部响应。请打印结果。
分别在(“结束”、()=>{
log(JSON.parse(data.explauration);
完成();
});
响应(“错误”、(错误)=>{
日志(“错误:+err.message”);
});
});
请求结束()
尝试在请求对象上添加.end()
const req=https.get(选项,(resp)=>{
让数据=”;
//已接收到一块数据。
分别在(“数据”、(块)=>{
数据+=块;
控制台日志(数据);
});
//已收到全部响应。请打印结果。
分别在(“结束”、()=>{
log(JSON.parse(data.explauration);
完成();
});
响应(“错误”、(错误)=>{
日志(“错误:+err.message”);
});
});
请求结束()
你有没有试过把完成
放在resp.on(“end”)
回调中?@yevheni是的,我得到了同样的结果。你有没有试过把完成
放在resp.on(“end”)
回调中?@yevheni是的,我得到同样的结果