2015-10-06 48 views
0

所以即時嘗試構建2個獨立的應用程序1作爲後端(Laravel作爲REST api)和Angular應用程序作爲客戶端,最終這2個應用程序必須一起工作與單個網絡應用程序相同的域。在同一個域下運行AngularJs和Laravel應用程序

什麼即時試圖完成:

我的角度應用程序是從index.html的引導單頁的應用程序,所有的路由都通過角,除了處理/ API/*應通過Laravel應用程序處理。 即時通訊使用2個不同的應用程序,以建立更加動態的網絡應用程序,所以我可以輕鬆地更改我的後端框架和技術,並將每個應用程序作爲「獨立」更容易地測試。 我不想在我的響應頭中使用CORS,因爲我的REST API只服務於我的Angular應用程序,而不是其他應用程序,例如開發人員的API。 我想使用代理,將轉寄所有的請求都來自http://localhost:9100/api/ *到:http://localhost:9000/api/ *

首先IM運行在端口9000上運行Laravel:通過運行一飲而盡

php artisan serve --port 9000 
下端口9100

和角度的應用任務(的index.html是在路徑./src):

gulp.task('webserver', function(){ 
connect.server({ 
    root: './src', 
    port: 9100, 
    middleware: function(connect, o) { 
     var url = require('url'); 
     var proxy = require('proxy-middleware'); 
     var options = url.parse('http://localhost:9000/api'); 
     options.route = '/api'; 
     return [proxy(options)]; 
    } 
}); 
}); 

這兩個應用程序很好地工作作爲一個獨立的,但是當我嘗試瀏覽到: http://localhost:9100/api/v1/comments我收到的F ollowing錯誤:

錯誤:在errnoException連接ECONNREFUSED(net.js:904:11)在Object.afterConnect [按的onComplete](net.js:895:19)

我試圖調查的原因這個問題,有人說它連接到我的主機文件,所以我不得不添加該行: 127.0.0.1 localhost

但它不工作。 我嘗試了不同的任務,一飲而盡:

gulp.task('webserver', function() { 
gulp.src("./src") 
    .pipe(webserver({ 
     port: 9100, 
     livereload: true, 
     open: 'http://localhost:9100', 
     proxies: [ 
      { 
       source: '/api', target: 'http://localhost:9000/api' 
      } 
     ] 
    })); 
}); 

而且我收到完全相同的錯誤...

我的開發環境是Windows 10位的x64。

+0

你說laravel運行在9000端口爲什麼它會迴應: http:// localhost:9100/api/v1/comments 或者是你的角度網站的網址也是? – Pitchinnate

+0

,因爲我添加了一個代理,當我發送一個http請求到localhost:9100/api/v1/comments時,理論上我應該從localhosy:9000/api/v1/comments得到迴應,我這樣做是因爲我需要2個不同的應用在我知道的唯一方式下運行相同的域...我的角度應用程序運行在端口9100. – Lihai

回答

1

您是否嘗試過使用http-proxy-middleware而不是proxy-middleware

我遇到了與proxy-middleware相同的錯誤。 (Gulp browser-sync - redirect API request via proxy

Error: connect ECONNREFUSED 
    at errnoException (net.js:904:11) 
    at Object.afterConnect [as oncomplete] (net.js:895:19) 

結束了創建HTTP代理中間件,解決問題在我的情況。

proxy-middleware莫名其妙地沒有在公司網絡上工作。 http-proxy只是。 (http-proxy-middleware使用http-proxy來做實際的代理)

猜你正在使用gulp-webserver;代理可以加入這樣的:

var proxyMiddleware = require('http-proxy-middleware'); 

gulp.task('webserver', function() { 
gulp.src("./src") 
    .pipe(webserver({ 
     port: 9100, 
     livereload: true, 
     open: 'http://localhost:9100', 
     middleware: [proxyMiddleware('/api', {target: 'http://localhost:9000'})] 
    })); 
}); 

從來沒有發現爲什麼這個錯誤是在企業網絡中使用代理中間件拋出...


更新:

認爲這個問題已經有了答案:

It was a problem with the artisan server, had to run it this way:

php artisan serve --host 0.0.0.0

來源:

https://github.com/chimurai/http-proxy-middleware/issues/38

https://github.com/chimurai/http-proxy-middleware/issues/21#issuecomment-138132809

+0

它也不工作,即時通訊接收未解決的錯誤:[HPM]代理錯誤:ECONNREFUSED。 undefined - >「localhost:9000/api/v1/comments」 在控制檯中。並在瀏覽器中顯示以下錯誤:「嘗試代理到:localhost:9000/api/v1/comments時發生錯誤」。完全控制檯輸出:[21:16:02]啓動'webserver'... [HPM]代理創建:/ api/v1 - > http:// localhost:9000/api/v1 [21:16:02] Web服務器在http:// localhost:9100開始 [21:16:02] 9.33 ms後完成'webserver' [HPM]代理錯誤:ECONNREFUSED。 undefined - >「localhost:9000/api/v1/comments」 – Lihai

+0

我根據這個問題找到了原因:https://github.com/chimurai/http-proxy-middleware/issues/21我不得不跑Laravel帶有'主機'標誌的服務器。然而,即時通訊從Laravel收到錯誤:「NotFoundHttpException」當我導航到:localhost:9100/api/v1/comments – Lihai

+0

也許設置'changeOrigin'爲'true'可能有助於 'proxyMiddleware('/ api',{target :'http:// localhost:9000',changeOrigin:true}' – chimurai

相關問題