账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    koa中需要get请求别的服务器只能用原生的http吗?
    28
    0
    app.use(async ctx => {
      console.log('ctx',ctx)  
      if ("/qsonhs.aspx" == ctx.path) {
        http
          .get("http://nodejs.cn/index.json", res => {
            const { statusCode } = res;
            const contentType = res.headers["content-type"];
    
            let error;
            if (statusCode !== 200) {
              error = new Error("请求失败\n" + `状态码: ${statusCode}`);
            } else if (!/^application\/json/.test(contentType)) {
              error = new Error(
                "非法的 content-type.\n" +
                  `期望的是 application/json 但接收到的是 ${contentType}`
              );
            }
            if (error) {
              console.error(error.message);
              // 消费响应的数据以释放内存。
              res.resume();
              return;
            }
    
            res.setEncoding("utf8");
            let rawData = "";
            res.on("data", chunk => {
              rawData += chunk;
            });
            res.on("end", () => {
              try {
                const parsedData = JSON.parse(rawData);
                console.log(parsedData);
              } catch (e) {
                console.error(e.message);
              }
            });
          })
          .on("error", e => {
            console.error(`报错: ${e.message}`);
          });
      }
    });
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部