先後端分離,本地前端開發調用接口會有跨域問題,通常有如下3種解決方法:javascript
1. 後端接口打包到本地運行(缺點:每次後端更新都要去測試服下一個更新包,還要在本地搭建java運行環境,麻煩)css
2. CORS跨域:後端接口在返回的時候,在header中加入'Access-Control-Allow-origin':* 之類的(有的時候後端不方便這樣處理,前端就蛋疼了)html
3. 用nodejs搭建本地http服務器,而且判斷訪問接口URL時進行轉發,完美解決本地開發時候的跨域問題。前端
4. 使用谷歌的插件解決:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbijava
5. 或者谷歌開啓容許跨域,參考 http://camnpr.com/archives/chrome-args-disable-web-security.htmlnode
1. nodejs搭建本地http服務器git
2. 應用node-http-proxy,作接口url的轉發github
1. node.js搭建本地http服務器參考了shawn.xie的《nodejs搭建本地http服務器》web
2. node.js作轉發使用node-http-proxy實現,官方文檔:https://github.com/nodejitsu/node-http-proxy#using-httpschrome
3. 操做方法參考了:http://hao.jser.com/archive/10394/?utm_source=tuicool&utm_medium=referral
4. 下面是我本身的實戰操做
1. npm初始化
npm init
2. 安裝node-http-proxy模塊
npm install http-proxy --save-dev
3. 項目結構
下面的例子中,咱們把html文件直接放在根目錄'./',也能夠指定一個網站目錄,在proxy.js中能夠自定義
var PORT = 3000; var http = require('http'); var url=require('url'); var fs=require('fs'); var mine=require('./mine').types; var path=require('path'); var httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer({ target: 'http://192.168.10.38:8180/', //接口地址 // 下面的設置用於https // ssl: { // key: fs.readFileSync('server_decrypt.key', 'utf8'), // cert: fs.readFileSync('server.crt', 'utf8') // }, // secure: false }); proxy.on('error', function(err, req, res){ res.writeHead(500, { 'content-type': 'text/plain' }); console.log(err); res.end('Something went wrong. And we are reporting a custom error message.'); }); var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; //var realPath = path.join("main-pages", pathname); // 指定根目錄 var realPath = path.join("./", pathname); console.log(pathname); console.log(realPath); var ext = path.extname(realPath); ext = ext ? ext.slice(1) : 'unknown'; //判斷若是是接口訪問,則經過proxy轉發 if(pathname.indexOf("mspj-mall-admin") > 0){ proxy.web(request, response); return; } fs.exists(realPath, function (exists) { if (!exists) { response.writeHead(404, { 'Content-Type': 'text/plain' }); response.write("This request URL " + pathname + " was not found on this server."); response.end(); } else { fs.readFile(realPath, "binary", function (err, file) { if (err) { response.writeHead(500, { 'Content-Type': 'text/plain' }); response.end(err); } else { var contentType = mine[ext] || "text/plain"; response.writeHead(200, { 'Content-Type': contentType }); response.write(file, "binary"); response.end(); } }); } }); }); server.listen(PORT); console.log("Server runing at port: " + PORT + ".");
這裏參考shawn.xie的源碼,補充了幾個字體文件的mime。
exports.types = { "css": "text/css", "gif": "image/gif", "html": "text/html", "ico": "image/x-icon", "jpeg": "image/jpeg", "jpg": "image/jpeg", "js": "text/javascript", "json": "application/json", "pdf": "application/pdf", "png": "image/png", "svg": "image/svg+xml", "swf": "application/x-shockwave-flash", "tiff": "image/tiff", "txt": "text/plain", "wav": "audio/x-wav", "wma": "audio/x-ms-wma", "wmv": "video/x-ms-wmv", "xml": "text/xml", "woff": "application/x-woff", "woff2": "application/x-woff2", "tff": "application/x-font-truetype", "otf": "application/x-font-opentype", "eot": "application/vnd.ms-fontobject" };
以上就是所有源碼
而後把項目中的接口地址改爲http://localhost:3000/......
啓動cmd,定位到項目目錄,運行
node proxy.js
訪問:
http://localhost:3000/index.html
能夠看到項目中調用的http://localhost:3000/..... 都會從http://192.168.10.38:8180/...... 獲取數據,而後轉發到本地。
這樣就不存在跨域了。