{"id":10006852,"date":"2022-11-17T00:01:54","date_gmt":"2022-11-16T16:01:54","guid":{"rendered":"https:\/\/ityet.com\/?p=10006852"},"modified":"2022-11-17T00:01:54","modified_gmt":"2022-11-16T16:01:54","slug":"%e4%bd%bf%e7%94%a8nodejs%e7%9a%84http%e5%92%8chttps%e4%b8%8b%e8%bd%bd%e8%bf%9c%e7%a8%8b%e8%b5%84%e6%ba%90","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006852","title":{"rendered":"\u4f7f\u7528nodejs\u7684http\u548chttps\u4e0b\u8f7d\u8fdc\u7a0b\u8d44\u6e90"},"content":{"rendered":"<p>\u7ecf\u5e38\u7528\u5230nodejs\u4e0b\u8f7d\u8d44\u6e90\u7684\u60c5\u51b5\uff08\u7b80\u5355\u7684\u722c\u866b\uff09\uff0c\u53ef\u4ee5\u8003\u8651\u76f4\u63a5\u4f7f\u7528nodejs\u5185\u7f6e\u7684http\/https\u6a21\u5757\u3002<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc3OY4\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>test.mjs<\/h3>\n<pre><code class=\"javascript\">import https from 'https'\nimport fs from 'fs'\n\nhttps.get({\n    rejectUnauthorized: false, \/\/ \u5ffd\u7565https\u5b89\u5168\u6027\n    host: 'a.com',         \/\/ \u8d44\u6e90host\uff5c\u4ee3\u7406\u670d\u52a1\u5668\n    port: 443,          \/\/ \u8d44\u6e90\u7aef\u53e3\uff5c\u4ee3\u7406\u670d\u52a1\u5668\u7aef\u53e3\n    method: 'GET',        \/\/ \u8bf7\u6c42\u65b9\u5f0f\n    path: '\/xx.jpg',    \/\/ \u8d44\u6e90\u5730\u5740\uff1ahttps:\/\/a.com\/xx.jpg\n    headers: {\n        referer: '',    \/\/ \u5982\u679c\u8d44\u6e90\u6709\u9632\u76d7\u94fe\uff0c\u5219\u6e05\u7a7a\u8be5\u5c5e\u6027\n    },\n}, res =&gt; {\n    \/\/\u8bbe\u7f6e\u7f16\u7801\u683c\u5f0f\n    res.setEncoding('binary');\n    let img = ''\n    res.on('data', chunk =&gt; {\n        img += chunk\n    })\n    res.on('end', chunk =&gt; {\n        \/\/ \u5199\u5230\u672c\u5730\uff0c\uff08\u6587\u4ef6\u540d\uff0c\u6e90\u6587\u4ef6\uff0c\u7f16\u7801\u683c\u5f0f\uff09\n        fs.writeFileSync('.\/test.jpg', img, &quot;binary&quot;);\n    })\n})<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042817539\">https:\/\/segmentfault.com\/a\/1190000042817539<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ecf\u5e38\u7528\u5230nodejs\u4e0b\u8f7d\u8d44\u6e90\u7684\u60c5\u51b5\uff08\u7b80\u5355\u7684\u722c\u866b\uff09\uff0c\u53ef\u4ee5\u8003\u8651\u76f4\u63a5\u4f7f\u7528nodejs\u5185\u7f6e\u7684http\/https\u6a21\u5757\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006852","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006852"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006852\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}