{"id":2009,"date":"2022-08-10T22:38:35","date_gmt":"2022-08-10T14:38:35","guid":{"rendered":"https:\/\/yukihiew.com\/?p=2009"},"modified":"2022-08-10T22:38:40","modified_gmt":"2022-08-10T14:38:40","slug":"devserver-proxy","status":"publish","type":"post","link":"https:\/\/yukihiew.com\/devserver-proxy\/","title":{"rendered":"Vue | \u4f7f\u7528devServer\u9032\u884c\u4ee3\u7406\u8de8\u57df \u524d\u7aef\u958b\u767c\u597d\u529f\u80fd"},"content":{"rendered":"
\u524d\u5f8c\u7aef\u5206\u96e2\u7684\u5c08\u6848\u5728\u958b\u767c\u6642\uff0c\u524d\u7aef\u6703\u9700\u8981\u81ea\u5df1\u4e32\u63a5api\u53bb\u6e2c\u8a66\u8cc7\u6599\u5eab\u6293\u8cc7\u6599\u4f86\u524d\u7aef\u770b\u770b\u756b\u9762\u6709\u7121\u9700\u8981\u6539\u9032\u6216bug\u7684\u5730\u65b9\uff0c\u4f46\u56e0\u70ba\u662f\u958b\u767c\u968e\u6bb5\u524d\u5f8c\u7aef\u53ef\u80fd\u6703\u914d\u7f6e\u5728\u4e0d\u540c\u7684server\u9020\u6210\u8de8\u7db2\u57dfCORS\u7684\u554f\u984c\u3002 Vue CLi \u88e1 vue.config.js \u914d\u7f6e\u7684 dev-server \u6240\u63d0\u4f9b\u7684 proxy \u529f\u80fd\uff0c\u900f\u904e\u8a2d\u5b9a devServer \u53ef\u4ee5\u5728\u958b\u767c\u6642\u81ea\u52d5\u9032\u884c\u8de8\u57df\u8655\u7406: \u5e38\u7528\u9078\u9805\u914d\u7f6e\u8207\u89e3\u91cb \u540c\u6642\u53ef\u642d\u914d .env \u74b0\u5883\u8b8a\u6578\u81ea\u8a02\u9700\u8de8\u57df\u7684 api \u5b57\u4e32\u53ca\u63a5\u53e3\uff0c\u5728\u958b\u767c\u6642\u4f5c\u8b8a\u66f4\u4e5f\u6bd4\u8f03\u65b9\u4fbf\u3002 \u9019\u662fYUKI\u5c08\u6848\u4e0a\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u53c3\u8003\u770b\u770b<\/p>\n\n\n\n http-proxy-middleware\u5be6\u73fe\u7684\u539f\u7406\u8acb\u770b\u9019(\u542breact\u7528\u6cd5): \u8981\u89e3\u6c7a\u7684\u554f\u984c \u524d\u5f8c\u7aef\u5206\u96e2\u7684\u5c08\u6848\u5728\u958b\u767c\u6642\uff0c\u524d\u7aef\u6703\u9700\u8981\u81ea\u5df1\u4e32\u63a5api\u53bb\u6e2c\u8a66\u8cc7\u6599\u5eab\u6293\u8cc7\u6599\u4f86\u524d\u7aef\u770b\u770b\u756b\u9762\u6709\u7121\u9700\u8981\u6539\u9032\u6216 […]<\/p>\n","protected":false},"author":1,"featured_media":2011,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[124],"tags":[123],"class_list":["post-2009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-124","tag-vue","entry","has-media"],"yoast_head":"\n
https:\/\/developer.mozilla.org\/zh-TW\/docs\/Web\/HTTP\/CORS<\/a><\/p>\n\n\n\u958b\u767c\u6642\u53ef\u4f7f\u7528<\/h2>\n\n\n
\u76f4\u63a5\u6307\u5411 api URL\uff0c\u5c31\u53ef\u5c07api URL\u4ee3\u7406\u5230\u672c\u6a5fport :<\/p>\n\n\n\n\/\/ vue.config.js\n devServer: {\n proxy: {\n '\/api': {\n target: ''https:\/\/localhost:5123\/api\/',\n ws: true,\n changeOrigin: true\n },\n '\/foo': {\n target: 'http:\/\/localhost:8081'\n }\n }\n }<\/code><\/pre>\n\n\n\n
\u7d30\u7bc0\u67e5\u770b https:\/\/webpack.js.org\/configuration\/dev-server\/#devserver<\/a><\/p>\n\n\n\n\/\/ \u958b\u767c\u914d\u7f6e\n devServer: {\n open: false,\/\/open \u5728devServer\u555f\u52d5\u4e14\u7b2c\u4e00\u6b21\u69cb\u5efa\u5b8c\u6210\u6642\uff0c\u81ea\u52d5\u7528\u6211\u5011\u7684\u7cfb\u7d71\u7684\u9810\u8a2d\u700f\u89bd\u5668\u53bb\u958b\u555f\u8981\u958b\u767c\u7684\u7db2\u9801\n host: '0.0.0.0',\/\/\u9810\u8a2d\u662f localhost\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4f3a\u670d\u5668\u5916\u90e8\u53ef\u8a2a\u554f\uff0c\u6307\u5b9a\u5982\u4e0b host: '0.0.0.0'\uff0c\u8a2d\u5b9a\u4e4b\u5f8c\u4e4b\u5f8c\u53ef\u4ee5\u8a2a\u554fip\u5730\u5740\n port: 8080,\n public: 'http:\/\/localhost:8080',\n hot: true,\/\/hot\u914d\u7f6e\u662f\u5426\u555f\u7528\u6a21\u7d44\u7684\u71b1\u66ff\u63db\u529f\u80fd\uff0cdevServer\u7684\u9810\u8a2d\u884c\u70ba\u662f\u5728\u767c\u73fe\u539f\u59cb\u78bc\u88ab\u8b8a\u66f4\u5f8c\uff0c\u901a\u904e\u81ea\u52d5\u91cd\u65b0\u6574\u7406\u6574\u500b\u9801\u9762\u4f86\u505a\u5230\u4e8b\u5be6\u9810\u89bd\uff0c\u958b\u555fhot\u5f8c\uff0c\u5c07\u5728\u4e0d\u91cd\u65b0\u6574\u7406\u6574\u500b\u9801\u9762\u7684\u60c5\u6cc1\u4e0b\u901a\u904e\u65b0\u6a21\u7d44\u66ff\u63db\u8001\u6a21\u7d44\u4f86\u505a\u5230\u5be6\u6642\u9810\u89bd\u3002\n https: false,\n hotOnly: false,\/\/ hot \u548c hotOnly \u7684\u5340\u5225\u662f\u5728\u67d0\u4e9b\u6a21\u7d44\u4e0d\u652f\u63f4\u71b1\u66f4\u65b0\u7684\u60c5\u6cc1\u4e0b\uff0c\u524d\u8005\u6703\u81ea\u52d5\u91cd\u65b0\u6574\u7406\u9801\u9762\uff0c\u5f8c\u8005\u4e0d\u6703\u91cd\u65b0\u6574\u7406\u9801\u9762\uff0c\u800c\u662f\u5728\u63a7\u5236\u6aaf\u8f38\u51fa\u71b1\u66f4\u65b0\u5931\u6557\n proxy: {\n '\/api\/': {\n target: 'https:\/\/localhost:5123\/api\/', \/\/\u8acb\u6c42\u8de8\u57df\u7684\u76ee\u6a19url\n secure: false, \/\/false\u70bahttp\u8a2a\u554f\uff0ctrue\u70bahttps\u8a2a\u554f\n changeOrigin: true, \/\/\u662f\u5426\u9032\u884c\u8de8\u57df\u8655\u7406\n pathRewrite: {\n '^\/api\/': '' \/\/\u91cd\u5beb\u8acb\u6c42\u76ee\u6a19\u7684url\n },\n ws:true\n }\n }, \/\/ \u8a2d\u5b9a\u4ee3\u7406\n }<\/code><\/pre>\n\n\n\n
Vue CLi \u5b98\u65b9\u6587\u4ef6:
https:\/\/cli.vuejs.org\/zh\/config\/#devserver-proxy<\/a><\/p>\n\n\n\u5be6\u969b\u7bc4\u4f8b\u914d\u7f6e<\/h2>\n\n\n
<\/figure>\n\n\n\n
\u66f4\u591a\u53c3\u8003<\/h2>\n\n\n
https:\/\/segmentfault.com\/a\/1190000038374436<\/a>
https:\/\/www.cnblogs.com\/zhaoweikai\/p\/9969282.html<\/a>
\u5169\u4f4d\u5927\u5927\u90fd\u5beb\u5f97\u5f88\u597d\uff0c\u53ef\u4ee5\u5b78\u7fd2\u5f88\u591a^^<\/p>\n","protected":false},"excerpt":{"rendered":"