当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Vue前后端不同端口的实现方法

Vue前后端不同端口的实现方法

2018年09月21日  | 移动技术网IT编程  | 我要评论

黛西之钻,旌德天气,www.selao.org

前端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前后端不同端口的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网