{"id":1987,"date":"2023-09-27T17:45:53","date_gmt":"2023-09-27T09:45:53","guid":{"rendered":"https:\/\/white-night.club\/?p=1987"},"modified":"2023-11-09T11:07:52","modified_gmt":"2023-11-09T03:07:52","slug":"appdev12","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/09\/27\/appdev12\/","title":{"rendered":"Vite\u914d\u7f6eProxy(\u540e\u7aefAPI)"},"content":{"rendered":"\n<p>\u5728Vite\u4e2d\u901a\u8fc7proxy\u914d\u7f6e\u540e\u7aefAPI\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p><em>\u914d\u7f6evite.config.js<\/em><\/p>\n<cite><em>proxyd\uff08\u4ee3\u7406\uff09\u7684\u914d\u7f6e<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u524d\u60c5\u63d0\u8981\uff1a\u6700\u8fd1\u5728\u505a\u767b\u5f55\u754c\u9762\uff0c\u4f46\u662f\u5728\u524d\u540e\u7aef\u6570\u636e\u4ea4\u4e92\u7684\u65f6\u5019\u78b0\u5230\u4e86\u95ee\u9898\u3002\u6bd4\u5982\u6211\u540e\u7aef\u7528\u7684spring boot\uff0c\u76d1\u542c\u7684\u662f3000\u7aef\u53e3\uff0c\u524d\u7aefvue\u9875\u9762\u7528\u7684\u662f5173\u7aef\u53e3\u3002\u4e24\u4e2a\u7684URL\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;localhost:5173\nhttp:\/\/localhost:3000\/user\/login<\/code><\/pre>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u4e24\u8005\u7684\u7aef\u53e3\u4e0d\u4e00\u6837\uff0c\u76f4\u63a5\u8dd1\u7684\u8bdd\u4f1a\u5bfc\u81f4CORS\u9519\u8bef\u3002\u4e4b\u524d\u7684\u6587\u7ae0\u6709\u63d0\u5230\u8fc7CORS\u662f\u5565\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u63d0\u4e86\u3002<\/p>\n\n\n\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u90a3\u5c31\u9700\u8981\u81ea\u5df1\u914d\u7f6e\u4e00\u4e2aproxy\uff08\u4ee3\u7406\uff09\u3002\u6211\u7684vite.config.js\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { fileURLToPath, URL } from 'node:url'\n\nimport { defineConfig } from 'vite'\nimport vue from '@vitejs\/plugin-vue'\n\n\/\/ https:\/\/vitejs.dev\/config\/\nexport default defineConfig({\n  plugins: &#91;\n    vue(),\n  ],\n  server:{\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">    port:5173,\n    proxy: {\n      '\/api\/': {\n        target: 'http:\/\/127.0.0.1:8120',\n        changeOrigin: true,\n        <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ffdf00\" class=\"has-inline-color\">rewrite:(path)=&gt;path.replace(\/^\\\/api\/,\"\")<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\n      }\n    }\n  },<\/mark>\n  base:'\/NightBook\/',\n  resolve: {\n    alias: {\n      '@': fileURLToPath(new URL('.\/src', import.meta.url))\n    }\n  }\n})\n<\/code><\/pre>\n\n\n\n<p>\u590d\u5236\u9ad8\u4eae\u7684\u90a3\u6bb5\u5230\u81ea\u5df1\u7684\u4ee3\u7801\u4e2d\u5c31\u884c\u3002\u987a\u5e26\u4e00\u63d0\u91cd\u5199\u8def\u5f84\u7684\u90a3\u884c\u4ee3\u7801\u4e0d\u8981\u5199\u9519\u4e86\u3002\u4e4b\u524d\u5199\u9519\u4e86\u5bfc\u81f4\u914d\u7f6e\u7684proxy\u4e0d\u751f\u6548\uff0c\u4e00\u76f4\u6ca1\u6cd5\u89e3\u51b3CORS\u9519\u8bef\uff0c\u5230\u5904\u627ebug\u4e5f\u627e\u4e0d\u51fa\u6765&#8230;.\u540e\u9762\u770b\u4e86\u522b\u4eba\u662f\u600e\u4e48\u5199\u7684\u624d\u53d1\u73b0\u81ea\u5df1\u6f0f\u6cc4\u4e86&#8217;\/&#8217;&#8230;.<\/p>\n\n\n\n<p>\u914d\u7f6e\u5b8c\u4e4b\u540e\u53ef\u4ee5\u91cd\u542f\u4e0bvue\u7684dev server\u3002\u91cd\u542f\u540e\u6d4b\u8bd5\u4e0b\u540e\u7aef\u5c31\u80fd\u6536\u5230\u524d\u7aef\u53d1\u7684\u8bf7\u6c42\u4e86\uff0cdone\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u51b3CORS\u62a5\u9519\u4e0e\u914d\u7f6e\u540e\u7aefAPI<\/p>\n","protected":false},"author":1,"featured_media":6608,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[72,51],"tags":[73],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1987"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=1987"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1987\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/6608"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=1987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=1987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=1987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}