{"id":25243,"date":"2024-11-21T13:00:49","date_gmt":"2024-11-21T05:00:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25243\/"},"modified":"2024-11-21T13:00:49","modified_gmt":"2024-11-21T05:00:49","slug":"express%e8%bf%98%e6%98%aflaravel%ef%bc%9f%e9%80%89%e6%8b%a9%e6%9c%80%e9%80%82%e5%90%88%e4%bd%a0%e7%9a%84%e5%90%8e%e7%ab%af%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25243\/","title":{"rendered":"Express\u8fd8\u662fLaravel\uff1f\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u540e\u7aef\u6846\u67b6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/171006516554781.jpg\" class=\"aligncenter\" title=\"Express\u8fd8\u662fLaravel\uff1f\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u540e\u7aef\u6846\u67b6\u63d2\u56fe\" alt=\"Express\u8fd8\u662fLaravel\uff1f\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u540e\u7aef\u6846\u67b6\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u9009\u62e9\u540e\u7aef\u6846\u67b6\u65f6\uff0cExpress\u548cLaravel\u90fd\u662f\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\u3002Express\u662f\u57fa\u4e8eNode.js\u7684Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\uff0c\u800cLaravel\u662f\u57fa\u4e8ePHP\u7684Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\u3002\u4e24\u8005\u5404\u6709\u4f18\u52bf\uff0c\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u7684\u6846\u67b6\u9700\u8981\u8003\u8651\u591a\u65b9\u9762\u56e0\u7d20\u3002<\/p>\n<p>Express\u6846\u67b6\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7075\u6d3b\u6027\u548c\u7b80\u5355\u7684\u5b66\u4e60\u66f2\u7ebf\u3002Express\u7684\u6838\u5fc3\u601d\u60f3\u662f\u201c\u8db3\u591f\u5c0f\uff0c\u8db3\u591f\u7075\u6d3b\u201d\uff0c\u5b83\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u4e2d\u95f4\u4ef6\u548c\u63d2\u4ef6\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u81ea\u7531\u9009\u62e9\u548c\u914d\u7f6e\u3002\u53e6\u5916\uff0c\u4f5c\u4e3aNode.js\u7684\u5e94\u7528\uff0cExpress\u5177\u6709\u9ad8\u6027\u80fd\u548c\u5f02\u6b65\u7f16\u7a0b\u7279\u6027\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528Express\u642d\u5efa\u4e00\u4e2aAPI\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u5f15\u5165Express\u6846\u67b6\nconst express = require('express');\nconst app = express();\nconst PORT = 3000;\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2aGET\u8bf7\u6c42\u7684\u8def\u7531\napp.get('\/', (req, res) =&amp;gt; {\n  res.send('Hello, Express!');\n});\n\n\/\/ \u542f\u52a8\u5e94\u7528\napp.listen(PORT, () =&amp;gt; {\n  console.log(`Server is running on port ${PORT}`);\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u6bd4\u4e4b\u4e0b\uff0cLaravel\u6846\u67b6\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u5b8c\u5907\u6027\u548c\u4f18\u96c5\u7684\u4ee3\u7801\u98ce\u683c\u3002Laravel\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u5305\u62ecORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u3001\u8def\u7531\u3001\u6a21\u677f\u5f15\u64ce\u7b49\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u51fa\u529f\u80fd\u5b8c\u5584\u7684Web\u5e94\u7528\u3002\u6b64\u5916\uff0cLaravel\u8fd8\u6709\u826f\u597d\u7684\u6587\u6863\u548c\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301\uff0c\u4fbf\u4e8e\u5f00\u53d1\u8005\u5b66\u4e60\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Laravel\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684RESTful API\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u5b9a\u4e49\u8def\u7531\nRoute::get('\/hello', function () {\n    return ['message' =&amp;gt; 'Hello, Laravel!'];\n});\n\n\/\/ \u5728\u63a7\u5236\u5668\u4e2d\u5904\u7406\u903b\u8f91\nclass HelloController extends Controller {\n    public function index() {\n        return response()-&amp;gt;json(['message' =&amp;gt; 'Hello, Laravel!']);\n    }\n}\n\n\/\/ \u6ce8\u518c\u8def\u7531\u5230\u63a7\u5236\u5668\u65b9\u6cd5\nRoute::get('\/hello', 'HelloController@index');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u9009\u62e9Express\u8fd8\u662fLaravel\u65f6\uff0c\u9700\u8981\u8003\u8651\u81ea\u5df1\u7684\u6280\u672f\u6808\u3001\u9879\u76ee\u9700\u6c42\u548c\u56e2\u961f\u7ecf\u9a8c\u3002\u5982\u679c\u5df2\u7ecf\u719f\u6089\u4e86Node.js\u548cJavaScript\uff0c\u4e14\u9700\u8981\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684API\u670d\u52a1\uff0c\u90a3\u4e48Express\u53ef\u80fd\u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002\u800c\u5982\u679c\u5bf9PHP\u8f83\u4e3a\u719f\u6089\uff0c\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u3001\u6613\u7ef4\u62a4\u7684Web\u5e94\u7528\uff0c\u90a3\u4e48Laravel\u53ef\u80fd\u66f4\u9002\u5408\u3002<\/p>\n<p>\u65e0\u8bba\u9009\u62e9Express\u8fd8\u662fLaravel\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u7075\u6d3b\u5e94\u7528\uff0c\u7ed3\u5408\u5404\u81ea\u7684\u4f18\u52bf\u6765\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u9879\u76ee\u8d28\u91cf\u3002\u5e0c\u671b\u4ee5\u4e0a\u7684\u4ee3\u7801\u793a\u4f8b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u4e24\u8005\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u7279\u70b9\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fExpress\u8fd8\u662fLaravel\uff1f\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u540e\u7aef\u6846\u67b6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9009\u62e9\u540e\u7aef\u6846\u67b6\u65f6\uff0cExpress\u548cLaravel\u90fd\u662f\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\u3002Express\u662f\u57fa\u4e8eNode.js\u7684Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\uff0c\u800cLaravel\u662f\u57fa\u4e8ePHP\u7684Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\u3002\u4e24\u8005\u5404\u6709\u4f18\u52bf\uff0c\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u7684\u6846\u67b6\u9700\u8981\u8003\u8651\u591a\u65b9\u9762\u56e0\u7d20\u3002 Express\u6846\u67b6\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7075\u6d3b\u6027\u548c\u7b80\u5355\u7684\u5b66\u4e60\u66f2\u7ebf\u3002Express\u7684\u6838\u5fc3\u601d\u60f3\u662f\u201c\u8db3\u591f\u5c0f\uff0c\u8db3\u591f\u7075\u6d3b\u201d\uff0c\u5b83\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u4e2d\u95f4\u4ef6\u548c\u63d2\u4ef6\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u81ea\u7531\u9009\u62e9\u548c\u914d\u7f6e\u3002\u53e6\u5916\uff0c\u4f5c\u4e3aNode.js\u7684\u5e94\u7528\uff0cExpress\u5177\u6709\u9ad8\u6027\u80fd\u548c\u5f02\u6b65\u7f16\u7a0b\u7279\u6027\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528Express\u642d\u5efa\u4e00\u4e2aAPI\u7684\u793a\u4f8b\uff1a \/\/ \u5f15\u5165Express\u6846\u67b6 const express = require(&#8216;express&#8217;); const app = express(); const PORT = 3000; \/\/ \u5b9a\u4e49\u4e00\u4e2aGET\u8bf7\u6c42\u7684\u8def\u7531 app.get(&#8216;\/&#8217;, (req, res) =&amp;gt; { res.send(&#8216;Hello, Express!&#8217;); }); \/\/ \u542f\u52a8\u5e94\u7528 app.listen(PORT, () =&amp;gt; { console.log(`Server is running on port ${PORT}`); }); \u767b\u5f55\u540e\u590d\u5236 \u76f8\u6bd4\u4e4b\u4e0b\uff0cLaravel\u6846\u67b6\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u5b8c\u5907\u6027\u548c\u4f18\u96c5\u7684\u4ee3\u7801\u98ce\u683c\u3002Laravel\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u5305\u62ecORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u3001\u8def\u7531\u3001\u6a21\u677f\u5f15\u64ce\u7b49\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u51fa\u529f\u80fd\u5b8c\u5584\u7684Web\u5e94\u7528\u3002\u6b64\u5916\uff0cLaravel\u8fd8\u6709\u826f\u597d\u7684\u6587\u6863\u548c\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301\uff0c\u4fbf\u4e8e\u5f00\u53d1\u8005\u5b66\u4e60\u548c\u89e3\u51b3\u95ee\u9898\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Laravel\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684RESTful API\u7684\u793a\u4f8b\uff1a \/\/ \u5b9a\u4e49\u8def\u7531 Route::get(&#8216;\/hello&#8217;, function () { return [&#8216;message&#8217; =&amp;gt; &#8216;Hello, Laravel!&#8217;]; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-25243","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=25243"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25243\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}