{"id":6188,"date":"2023-01-07T11:30:11","date_gmt":"2023-01-07T03:30:11","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6188"},"modified":"2023-01-07T11:30:11","modified_gmt":"2023-01-07T03:30:11","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8%e8%b7%af%e7%94%b1routing","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8%e8%b7%af%e7%94%b1routing\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528\u8def\u7531(Routing)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, routing, \u8def\u7531\"><\/p>\n<h1>\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528\u8def\u7531(routing)\uff1f<\/h1>\n<p>Node.js \u662f\u4e00\u500b\u5efa\u7acb\u5728 Chrome V8 JavaScript \u5f15\u64ce\u4e0a\u7684 JavaScript \u74b0\u5883\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u5efa\u7acb\u9ad8\u6548\u80fd\u7684\u7db2\u8def\u61c9\u7528\u7a0b\u5f0f\u3002Node.js \u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u8def\u7531\u7cfb\u7d71\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u8def\u7531\uff1f<\/h2>\n<p>\u8def\u7531\u662f\u4e00\u7a2e\u5c07\u7db2\u8def\u8acb\u6c42\u5c0e\u5411\u6b63\u78ba\u7684\u61c9\u7528\u7a0b\u5f0f\u529f\u80fd\u7684\u6280\u8853\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\uff0c\u4e26\u5c07\u8acb\u6c42\u5c0e\u5411\u6b63\u78ba\u7684\u61c9\u7528\u7a0b\u5f0f\u529f\u80fd\u3002<\/p>\n<h2>Node.js \u8def\u7531\u7cfb\u7d71<\/h2>\n<p>Node.js \u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u8def\u7531\u7cfb\u7d71\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\uff0c\u4e26\u5c07\u8acb\u6c42\u5c0e\u5411\u6b63\u78ba\u7684\u61c9\u7528\u7a0b\u5f0f\u529f\u80fd\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528\u8def\u7531\uff1f<\/h2>\n<p>\u8981\u5728 Node.js \u4e2d\u4f7f\u7528\u8def\u7531\uff0c\u958b\u767c\u8005\u9700\u8981\u5148\u5b89\u88dd <strong>Express<\/strong> \u6846\u67b6\u3002Express \u662f\u4e00\u500b\u57fa\u65bc Node.js \u7684\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u5efa\u7acb\u7db2\u8def\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<p>\u5b89\u88dd\u5b8c Express \u4e4b\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 Express \u7684 <strong>Router<\/strong> \u4f86\u5efa\u7acb\u8def\u7531\u3002Router \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\uff0c\u4e26\u5c07\u8acb\u6c42\u5c0e\u5411\u6b63\u78ba\u7684\u61c9\u7528\u7a0b\u5f0f\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 Node.js \u4e2d\u4f7f\u7528\u8def\u7531\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst express = require('express');\nconst router = express.Router();\n\n\/\/ \u5b9a\u7fa9\u8def\u7531\nrouter.get('\/', (req, res) => {\n  res.send('Hello World!');\n});\n\n\/\/ \u5c07\u8def\u7531\u6302\u8f09\u5230\u61c9\u7528\u7a0b\u5f0f\nconst app = express();\napp.use('\/', router);\n\n\/\/ \u555f\u52d5\u4f3a\u670d\u5668\nconst port = 3000;\napp.listen(port, () => {\n  console.log(`Server started on port ${port}`);\n});\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 Express \u7684 Router \u4f86\u5b9a\u7fa9\u8def\u7531\uff0c\u4e26\u5c07\u8def\u7531\u6302\u8f09\u5230\u61c9\u7528\u7a0b\u5f0f\u4e0a\u3002\u7136\u5f8c\uff0c\u6211\u5011\u555f\u52d5\u4f3a\u670d\u5668\uff0c\u4e26\u5728\u700f\u89bd\u5668\u4e2d\u8f38\u5165 <code>http:\/\/localhost:3000\/<\/code>\uff0c\u5c31\u53ef\u4ee5\u770b\u5230 <code>Hello World!<\/code> \u7684\u8a0a\u606f\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cNode.js \u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u8def\u7531\u7cfb\u7d71\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\u3002\u8981\u5728 Node.js \u4e2d\u4f7f\u7528\u8def\u7531\uff0c\u958b\u767c\u8005\u9700\u8981\u5148\u5b89\u88dd Express \u6846\u67b6\uff0c\u4e26\u4f7f\u7528 Express \u7684 Router \u4f86\u5b9a\u7fa9\u8def\u7531\uff0c\u4e26\u5c07\u8def\u7531\u6302\u8f09\u5230\u61c9\u7528\u7a0b\u5f0f\u4e0a\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Node.js \u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u8def\u7531\u7cfb\u7d71\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u7db2\u8def\u8acb\u6c42\u505a\u51fa\u53cd\u61c9\u3002\u8981\u5728 Node.js \u4e2d\u4f7f\u7528\u8def\u7531\uff0c\u958b\u767c\u8005\u9700\u8981\u5148\u5b89\u88dd Express \u6846\u67b6\uff0c\u4e26\u4f7f\u7528 Express \u7684 Router \u4f86\u5b9a\u7fa9\u8def\u7531\uff0c\u4e26\u5c07\u8def\u7531\u6302\u8f09\u5230\u61c9\u7528\u7a0b\u5f0f\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u5728Node.js\u4e2d\u4f7f\u7528\u8def\u7531(Routing)\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u7684\u5efa\u7acb\u7db2\u7ad9\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528\u8def\u7531(Routing)\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5feb\u901f\u7684\u5efa\u7acb\u7db2\u7ad9\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-6188","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-1BO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6188","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=6188"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6188\/revisions"}],"predecessor-version":[{"id":6189,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6188\/revisions\/6189"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}