{"id":5759,"date":"2023-01-06T14:55:53","date_gmt":"2023-01-06T06:55:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5759"},"modified":"2023-01-06T14:55:53","modified_gmt":"2023-01-06T06:55:53","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%9c%8d%e5%8b%99%e7%ab%af%e6%b8%b2%e6%9f%93%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%9c%8d%e5%8b%99%e7%ab%af%e6%b8%b2%e6%9f%93%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, \u670d\u52d9\u7aef\u6e32\u67d3\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\uff1f<\/h1>\n<p>React \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5e8f\u7684 JavaScript \u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u670d\u52d9\u7aef\u6e32\u67d3\uff08SSR\uff09\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u8b93\u60a8\u5728\u670d\u52d9\u5668\u4e0a\u6e32\u67d3 React \u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u5c07\u6e32\u67d3\u7684\u7d50\u679c\u50b3\u905e\u7d66\u7528\u6236\u7aef\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u8a0e\u8ad6\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u670d\u52d9\u7aef\u6e32\u67d3\uff1f<\/h2>\n<p>\u670d\u52d9\u7aef\u6e32\u67d3\uff08SSR\uff09\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u8b93\u60a8\u5728\u670d\u52d9\u5668\u4e0a\u6e32\u67d3 React \u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u5c07\u6e32\u67d3\u7684\u7d50\u679c\u50b3\u905e\u7d66\u7528\u6236\u7aef\u3002\u8207\u5ba2\u6236\u7aef\u6e32\u67d3\u76f8\u6bd4\uff0c\u670d\u52d9\u7aef\u6e32\u67d3\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u6e1b\u5c11\u5ba2\u6236\u7aef\u7684\u8a08\u7b97\u91cf\uff0c\u4e26\u6e1b\u5c11\u5ba2\u6236\u7aef\u7684\u7db2\u7d61\u6d41\u91cf\u3002<\/p>\n<h2>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\uff1f<\/h2>\n<p>\u8981\u5728 React \u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\uff0c\u60a8\u9700\u8981\u4f7f\u7528 Node.js \u4f86\u69cb\u5efa\u670d\u52d9\u5668\u7aef\u61c9\u7528\u7a0b\u5e8f\u3002 Node.js \u662f\u4e00\u500b\u57fa\u65bc JavaScript \u7684\u670d\u52d9\u5668\u7aef\u958b\u767c\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u8b93\u60a8\u4f7f\u7528 JavaScript \u4f86\u69cb\u5efa\u670d\u52d9\u5668\u7aef\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 Node.js \u4f86\u69cb\u5efa\u4e00\u500b\u670d\u52d9\u5668\u7aef\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u4f7f\u7528 React \u4f86\u6e32\u67d3\u61c9\u7528\u7a0b\u5e8f\u7684 HTML\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u6e32\u67d3 React \u61c9\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst React = require('react');\nconst ReactDOMServer = require('react-dom\/server');\n\nconst App = require('.\/App');\n\nconst html = ReactDOMServer.renderToString(<App \/>);\n\nconsole.log(html);\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4f7f\u7528 ReactDOMServer \u6a21\u7d44\u4f86\u6e32\u67d3 React \u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u5c07\u6e32\u67d3\u7684 HTML \u5b57\u7b26\u4e32\u8f38\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u5c07\u6e32\u67d3\u7684 HTML \u5b57\u7b26\u4e32\u50b3\u905e\u7d66\u5ba2\u6236\u7aef\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst http = require('http');\n\nconst server = http.createServer((req, res) => {\n  res.writeHead(200, { 'Content-Type': 'text\/html' });\n  res.end(html);\n});\n\nserver.listen(3000);\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4f7f\u7528 Node.js \u7684 http \u6a21\u7d44\u4f86\u5275\u5efa\u4e00\u500b HTTP \u670d\u52d9\u5668\uff0c\u4e26\u5c07\u6e32\u67d3\u7684 HTML \u5b57\u7b26\u4e32\u50b3\u905e\u7d66\u5ba2\u6236\u7aef\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a0e\u8ad6\u4e86\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\u3002\u6211\u5011\u9996\u5148\u4ecb\u7d39\u4e86\u670d\u52d9\u7aef\u6e32\u67d3\u7684\u6982\u5ff5\uff0c\u7136\u5f8c\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Node.js \u4f86\u69cb\u5efa\u670d\u52d9\u5668\u7aef\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u4f7f\u7528 React \u4f86\u6e32\u67d3\u61c9\u7528\u7a0b\u5e8f\u7684 HTML\u3002\u6700\u5f8c\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5c07\u6e32\u67d3\u7684 HTML \u5b57\u7b26\u4e32\u50b3\u905e\u7d66\u5ba2\u6236\u7aef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u670d\u52d9\u7aef\u6e32\u67d3\uff0c\u5f9e\u6982\u5ff5\u5230\u5be6\u8e10\uff0c\u8b93\u4f60\u66f4\u52a0\u4e86\u89e3\u670d\u52d9\u7aef\u6e32\u67d3\u7684\u512a\u52e2\u548c\u7f3a\u9ede\uff0c\u4ee5\u53ca\u5982\u4f55\u5728React\u4e2d\u5be6\u73fe\u5b83\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":[184,183],"tags":[182,181],"class_list":["post-5759","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1uT","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5759","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=5759"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5759\/revisions"}],"predecessor-version":[{"id":5760,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5759\/revisions\/5760"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}