{"id":6358,"date":"2025-05-28T14:19:42","date_gmt":"2025-06-04T06:42:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6358"},"modified":"2025-06-04T14:19:42","modified_gmt":"2025-06-04T06:42:42","slug":"%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8https%e5%ba%ab%e9%80%b2%e8%a1%8c%e5%ae%89%e5%85%a8%e7%b6%b2%e7%b5%a1%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8https%e5%ba%ab%e9%80%b2%e8%a1%8c%e5%ae%89%e5%85%a8%e7%b6%b2%e7%b5%a1%e9%80%9a%e4%bf%a1\/","title":{"rendered":"\u5728 Node.js \u4e2d\u4f7f\u7528 HTTPS \u5eab\u9032\u884c\u5b89\u5168\u7db2\u7d61\u901a\u4fe1\uff1a2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, https, \u5b89\u5168\u7f51\u7edc\u901a\u4fe1, HTTPS, \u7db2\u7d61\u5b89\u5168\"><\/p>\n<h1>\u5728 Node.js \u4e2d\u4f7f\u7528 HTTPS \u5eab\u9032\u884c\u5b89\u5168\u7db2\u7d61\u901a\u4fe1<\/h1>\n<p>Node.js \u662f\u4e00\u500b\u5ee3\u53d7\u6b61\u8fce\u7684 JavaScript \u74b0\u5883\uff0c\u9069\u5408\u7528\u65bc\u958b\u767c\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002\u5728\u958b\u767c\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u5b89\u5168\u7db2\u7d61\u901a\u4fe1\u662f\u4e00\u500b\u81f3\u95dc\u91cd\u8981\u7684\u8003\u616e\u56e0\u7d20\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 HTTPS \u5eab\u4f86\u5be6\u73fe\u5b89\u5168\u7db2\u7d61\u901a\u4fe1\uff0c\u4e26\u63d0\u4f9b 2025 \u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f HTTPS\uff1f<\/h2>\n<p>HTTPS\uff08Hypertext Transfer Protocol Secure\uff09\u662f\u4e00\u7a2e\u5b89\u5168\u7684\u7db2\u7d61\u5354\u8b70\uff0c\u4f7f\u7528 SSL\/TLS \u52a0\u5bc6\u4f86\u4fdd\u8b77\u6578\u64da\u5728\u7db2\u7d61\u4e0a\u7684\u50b3\u8f38\u3002\u5b83\u80fd\u6709\u6548\u9632\u6b62\u99ed\u5ba2\u7684\u7aba\u63a2\u6216\u6578\u64da\u7be1\u6539\uff0c\u78ba\u4fdd\u7528\u6236\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<h2>\u5728 Node.js \u4e2d\u4f7f\u7528 HTTPS \u5eab\u7684\u6b65\u9a5f<\/h2>\n<p>\u5728 Node.js \u4e2d\u4f7f\u7528 HTTPS \u5eab\u5be6\u73fe\u5b89\u5168\u7db2\u7d61\u901a\u4fe1\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u6b65\u9a5f\uff1a<\/p>\n<p>1. **\u5b89\u88dd HTTPS \u6a21\u584a**\uff1a<br \/>\n   \u60a8\u53ef\u4ee5\u4f7f\u7528 npm \u5b89\u88dd HTTPS \u6a21\u584a\uff0c\u4ee5\u4e0b\u6307\u4ee4\u5c07\u5176\u5b89\u88dd\u5230\u60a8\u7684\u9805\u76ee\u4e2d\uff1a<\/p>\n<p>   &#8220;`bash<br \/>\n   npm install https<br \/>\n   &#8220;`<\/p>\n<p>2. **\u5275\u5efa HTTPS \u670d\u52d9\u5668**\uff1a<br \/>\n   \u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u5275\u5efa\u4e00\u500b HTTPS \u670d\u52d9\u5668\u3002\u78ba\u4fdd\u60a8\u5df2\u7d93\u64c1\u6709 SSL\/TLS \u8b49\u66f8\u548c\u79c1\u9470\u6587\u4ef6\u3002<\/p>\n<p>   &#8220;`javascript<br \/>\n   const https = require(&#8216;https&#8217;);<br \/>\n   const fs = require(&#8216;fs&#8217;);<\/p>\n<p>   const options = {<br \/>\n     key: fs.readFileSync(&#8216;path\/to\/your\/private-key.pem&#8217;),<br \/>\n     cert: fs.readFileSync(&#8216;path\/to\/your\/certificate.pem&#8217;)<br \/>\n   };<\/p>\n<p>   https.createServer(options, (req, res) => {<br \/>\n     res.writeHead(200);<br \/>\n     res.end(&#8216;Hello World\\n&#8217;);<br \/>\n   }).listen(8000, () => {<br \/>\n     console.log(&#8216;HTTPS Server running on port 8000&#8217;);<br \/>\n   });<br \/>\n   &#8220;`<\/p>\n<p>   \u5728\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `fs` \u6a21\u584a\u8b80\u53d6 SSL\/TLS \u8b49\u66f8\u548c\u79c1\u9470\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66 `https.createServer()` \u65b9\u6cd5\uff0c\u5f9e\u800c\u5275\u5efa\u4e00\u500b HTTPS \u670d\u52d9\u5668\u3002<\/p>\n<p>3. **\u767c\u9001 HTTPS \u8acb\u6c42**\uff1a<br \/>\n   \u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u767c\u9001 HTTPS \u8acb\u6c42\uff1a<\/p>\n<p>   &#8220;`javascript<br \/>\n   const https = require(&#8216;https&#8217;);<\/p>\n<p>   const options = {<br \/>\n     hostname: &#8216;www.example.com&#8217;,<br \/>\n     port: 443,<br \/>\n     path: &#8216;\/&#8217;,<br \/>\n     method: &#8216;GET&#8217;<br \/>\n   };<\/p>\n<p>   const req = https.request(options, (res) => {<br \/>\n     console.log(&#8216;statusCode:&#8217;, res.statusCode);<br \/>\n     console.log(&#8216;headers:&#8217;, res.headers);<\/p>\n<p>     res.on(&#8216;data&#8217;, (d) => {<br \/>\n       process.stdout.write(d);<br \/>\n     });<br \/>\n   });<\/p>\n<p>   req.on(&#8216;error&#8217;, (e) => {<br \/>\n     console.error(e);<br \/>\n   });<br \/>\n   req.end();<br \/>\n   &#8220;`<\/p>\n<p>   \u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `https.request()` \u65b9\u6cd5\u767c\u9001\u4e86\u4e00\u500b HTTPS \u8acb\u6c42\uff0c\u4e26\u5728 `res.on(&#8216;data&#8217;)` \u4e8b\u4ef6\u4e2d\u63a5\u6536\u5230\u4e86\u670d\u52d9\u5668\u7684\u97ff\u61c9\u6578\u64da\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6392\u9664\u932f\u8aa4\u7684\u5efa\u8b70\uff1a<\/p>\n<p>&#8211; **\u8b49\u66f8\u932f\u8aa4**\uff1a\u78ba\u4fdd\u60a8\u6240\u63d0\u4f9b\u7684\u8b49\u66f8\u548c\u79c1\u9470\u662f\u6b63\u78ba\u7684\uff0c\u4e14\u8def\u5f91\u7121\u8aa4\u3002<br \/>\n&#8211; **\u7aef\u53e3\u554f\u984c**\uff1a\u78ba\u4fdd\u60a8\u6240\u4f7f\u7528\u7684\u7aef\u53e3\uff08\u5982 8000\uff09\u6c92\u6709\u88ab\u5176\u4ed6\u61c9\u7528\u4f54\u7528\u3002<br \/>\n&#8211; **HTTPS \u8acb\u6c42\u5931\u6557**\uff1a\u6aa2\u67e5\u7db2\u7d61\u9023\u63a5\uff0c\u4e26\u78ba\u4fdd\u8acb\u6c42\u7684\u76ee\u6a19 URL \u662f\u6b63\u78ba\u7684\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684 HTTPS \u670d\u52d9\u5668\u548c\u8acb\u6c42\uff0c\u60a8\u9084\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u61c9\u7528\u3002\u4f8b\u5982\uff0c\u5be6\u73fe\u7528\u6236\u8eab\u4efd\u9a57\u8b49\u3001\u6578\u64da\u52a0\u5bc6\u6216\u8a2d\u7f6e\u81ea\u52d5\u91cd\u5b9a\u5411\u7b49\u529f\u80fd\uff0c\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u61c9\u7528\u7684\u5b89\u5168\u6027\u548c\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0c\u4f7f\u7528 HTTPS \u5eab\u53ef\u4ee5\u8f15\u9b06\u5730\u5728 Node.js \u4e2d\u5be6\u73fe\u5b89\u5168\u7db2\u7d61\u901a\u4fe1\u3002\u9019\u4e0d\u50c5\u80fd\u9632\u6b62\u8cc7\u6599\u88ab\u7aca\u53d6\uff0c\u4e5f\u80fd\u78ba\u4fdd\u7528\u6236\u7684\u4fe1\u4efb\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u6df1\u5165\u4e86\u89e3 Python \u7684\u5b89\u5168\u7de8\u7a0b\uff0c\u5efa\u8b70\u60a8\u67e5\u770b\u9019\u7bc7 [Python \u5b89\u5168\u7de8\u7a0b\u6700\u4f73\u5be6\u8e10](https:\/\/vocus.cc\/article\/\u4f60\u7684\u6587\u7ae0\u93c8\u63a5) \u7684\u6587\u7ae0\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5728 Node.js \u4e2d\u4f7f\u7528 HTTPS \u670d\u52d9\u5668\u9700\u8981 SSL \u8b49\u66f8\u55ce\uff1f**<br \/>\n\u662f\u7684\uff0c\u4f7f\u7528 HTTPS \u670d\u52d9\u5668\u6642\u9700\u8981\u6709\u6548\u7684 SSL \u8b49\u66f8\u548c\u79c1\u9470\uff0c\u9019\u662f\u78ba\u4fdd\u901a\u4fe1\u5b89\u5168\u7684\u95dc\u9375\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u7372\u53d6 SSL \u8b49\u66f8\uff1f**<br \/>\n\u60a8\u53ef\u4ee5\u901a\u904e\u8a31\u591a\u8b49\u66f8\u9812\u767c\u6a5f\u69cb\uff08CA\uff09\u7372\u53d6 SSL \u8b49\u66f8\uff0c\u6216\u8005\u4f7f\u7528 Let&#8217;s Encrypt \u63d0\u4f9b\u7684\u514d\u8cbb\u8b49\u66f8\u3002<\/p>\n<p>**Q3: HTTPS \u8207 HTTP \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nHTTPS \u662f HTTP \u7684\u5b89\u5168\u7248\u672c\uff0c\u4f7f\u7528 SSL\/TLS \u52a0\u5bc6\u4f86\u4fdd\u8b77\u6578\u64da\u50b3\u8f38\uff0c\u800c HTTP \u5247\u4e0d\u63d0\u4f9b\u9019\u7a2e\u5b89\u5168\u6027\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:Node.js\u63d0\u4f9b\u4e86\u4e00\u500b\u5b89\u5168\u7684\u7db2\u7d61\u901a\u4fe1\u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728Node.js\u4e2d\u4f7f\u7528HTTPS\u5354\u8b70\u9032\u884c\u5b89\u5168\u7684\u7db2\u7d61\u901a\u4fe1\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528HTTPS\u5eab\u9032\u884c\u5b89\u5168\u7db2\u7d61\u901a\u4fe1\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[193,192],"tags":[191,190],"class_list":["post-6358","post","type-post","status-publish","format-standard","hentry","category-node","category-node-js","tag-node","tag-node-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ey","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6358"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6358\/revisions"}],"predecessor-version":[{"id":6359,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6358\/revisions\/6359"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}