Vue前后端不同端口的实现方法
2019/6/27 7:41:33
本文主要是介绍Vue前后端不同端口的实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前端Vue 8080端口,后端Node.js 8085端口 主要记录下前后端不同端口遇到的问题
1、写服务器端程序,我的在(node_proxy/index.js)下
app.all('*', function (req, res, next) { res.header('Access-Control-Allow-Origin', req.headers.origin || '*'); res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization,\'Origin\',Accept,X-Requested-With'); res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.header('Access-Control-Allow-Credentials', true); res.header('X-Powered-By', ' 3.2.1'); res.header('Content-Type', 'application/json;charset=utf-8'); if (req.method === 'OPTIONS') { res.sendStatus(200); } else { next(); } });
这段代码很重要,要是没有的话会出现 No 'Access-Control-Allow-Origin' header is present on the requested resource 错误。
2、在前端用axios写get请求处理程序,写在了Card模板下
mounted(){ axios.get(HOST) .then(response => { this.sed = response.data.data; }) }
3、在config/index.js下配置proxyTable:
proxyTable: { '/seller': { target: 'http://localhost:8085', changeOrigin: true, pathRewrite: { '^/seller': '/seller' } } },
4、分别启动前后端,OK~\(≧▽≦)/~啦啦啦~
以上这篇Vue前后端不同端口的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于Vue前后端不同端口的实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set