Vue前后端不同端口的實現(xiàn)方法
前端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();
}
});
這段代碼很重要,要是沒有的話會出現(xiàn) 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前后端不同端口的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?cesium加載點與定位到指定位置的實現(xiàn)方法
Cesium是一個用于創(chuàng)建高性能、跨平臺的3D地球和地圖的開源JavaScript庫,它提供了許多功能,包括地理空間數(shù)據(jù)可視化、地理定位和地圖導航等,這篇文章主要介紹了vue?cesium加載點與定位到指定位置,需要的朋友可以參考下2024-03-03
關(guān)于vue-router的beforeEach無限循環(huán)的問題解決
本篇文章主要介紹了關(guān)于vue-router的beforeEach無限循環(huán)的問題解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09

