2014-02-07 137 views
3

我正在使用Rails作爲我的API,AngularJS在前端,而且我有一些問題正在使livereload/grunt連接代理正常工作。Grunt Livereload + Grunt連接代理

這裏是我的gruntfile片斷:

connect: { 
     options: { 
     port: 9000, 
     // Change this to '0.0.0.0' to access the server from outside. 
     hostname: 'localhost', 
     livereload: 35729 
     }, 
     proxies: [ 
     { 
      context: '/api', 
      host: 'localhost', 
      port: 3000 
     } 
     ], 
     livereload: { 
     options: { 
      open: true, 
      base: [ 
      '.tmp', 
      '<%= yeoman.app %>' 
      ], 
      middleware: function (connect, options) { 
      var middlewares = []; 
      var directory = options.directory || options.base[options.base.length - 1]; 

      // enable Angular's HTML5 mode 
      middlewares.push(modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png$ /index.html [L]'])); 

      if (!Array.isArray(options.base)) { 
       options.base = [options.base]; 
      } 
      options.base.forEach(function(base) { 
       // Serve static files. 
       middlewares.push(connect.static(base)); 
      }); 

      // Make directory browse-able. 
      middlewares.push(connect.directory(directory)); 

      return middlewares; 
      } 
     } 
     }, 
     test: { 
     options: { 
      port: 9001, 
      base: [ 
      '.tmp', 
      'test', 
      '<%= yeoman.app %>' 
      ] 
     } 
     }, 
     dist: { 
     options: { 
      base: '<%= yeoman.dist %>' 
     } 
     } 
    } 

如果我咕嚕建立「一切完美 - 關localhost:3000

但是,如果我的呼嚕聲服務」它通過127.0.0.1:9000,我打開了一扇窗獲取404給我所有的API調用。

此外,根據服務它是從一個CSS文件重整我的背景圖像我得到這樣的警告:

Resource interpreted as Image but transferred with MIME type text/html: "http://127.0.0.1:9000/images/RBP_BG.jpg" 

我以前沒有做過這一點 - 因此機會是我做這一切是錯誤的。

回答

5

我不喜歡connect.livereload.middleware配置中的代碼太多。
這是所有必要的嗎?

在我的一些項目中查看this commit - chore(yeoman-gruntfile-update): configured grunt-connect-proxy

+0

感謝您發佈此示例代碼咕嚕,連接代理 –

+0

你救我一命的傢伙! –

3

這是一箇舊帖子,但請確保您在livereload之前通過調用configureProxies實際初始化grunt serve任務中的代理。

grunt.task.run([ 
    'clean:server', 
    'bower-install', 
    'concurrent:server', 
    'autoprefixer', 
    'configureProxies', 
    'connect:livereload', 
    'watch' 
]); 

以後應該可以正常工作。

0

我和你有類似的問題,但我沒有使用yeoman。

我的解決方案是添加任務'configureProxies'。

這是我的任務:

grunt.registerTask( '服務',[ '連接:livereload', 'configureProxies', '開放:服務器', '手錶']);

和'connect:livereload','configureProxies' - 經過我的測試,這兩個任務的順序不會影響結果。

github grunt-connect-proxy