gulp通过http-proxy-middleware开启反向代理,实现跨域
生活随笔
收集整理的這篇文章主要介紹了
gulp通过http-proxy-middleware开启反向代理,实现跨域
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原理同nginx開啟代理,只不過寫法不同,所以直接上代碼:
1、gulpfile.js配置代理服務器
gulp.task("domain3",function(){webServer.server({root:"./crossDomainC",port: 8082,livereload: true,middleware:function(connect,opt){return [proxy("/api",{target:"https://api.douban.com/",changeOrigin:true,pathRewrite:{//路徑重寫規則 '^/api':''}})]}}); })2、服務器頁面index.html
<!DOCTYPE html> <html> <head><title>我是domain3</title> </head> <body><p>我是domain3</p> </body> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript">//請求本地js模擬后臺數據 $.ajax({type: "get",url: "/api/v2/movie/in_theaters",dataType: "dataType",success: function (res) {console.log(res)}});//請求本地js模擬后臺數據 $.ajax({type: "get",url: "/apis/index.js",dataType: "dataType",success: function (res) {console.log(res)}}); </script> </html>3、這里要注意的是,這只是粗淺的了解了下這個插件的功能,具體怎么用還得多多研究,所以這里暫時只找到了一種路由轉發的方法。配置里的return按理來說可以寫數組,具體怎么寫還不清楚。有興趣的朋友可以看看。
4、參考
?、賖ttps://www.jianshu.com/p/a248b146c55a;
?、趆ttps://blog.csdn.net/weixin_33712987/article/details/87071757
轉載于:https://www.cnblogs.com/helloNico/p/10688200.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的gulp通过http-proxy-middleware开启反向代理,实现跨域的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python猫荐书系列:文也深度学习,理
- 下一篇: Tomcat映射虚拟路径到指定磁盘(ec