2

背景:如何解決Gruntfile.js中錯誤配置的grunt-connect-proxy設置導致的404錯誤?

我試圖連接我的呼嚕聲服務器實例,在本地主機在同一臺機器上運行我的API服務:8080/API /。

當前使用grunt-connect-proxy來實現此目的。

問題/疑問:

http://localhost:9000/api/user-profile/ Failed to load resource: the server responded with a status of 404 (Not Found) 

是否與我的配置(如下圖),導致無法在本地主機重定向到代理服務器/api請求的錯誤:8080?

我的設置(Gruntfile.js):

var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest; 

... 

    // Grunt configuration 
    grunt.initConfig({ 

     // Project settings 
     someApp: appConfig, 

     // The grunt server settings 
     connect: { 
      options: { 
       port: 9000, 
       hostname: 'localhost', 
       livereload: 35729 
      }, 
      server: { 
       proxies: [ 
        { 
         context: '/api', 
         host: 'localhost', 
         port: 8080, 
         changeOrigin: true 
        } 
       ] 
      }, 
      livereload: { 
       options: { 
        open: true, 
        middleware: function (connect) { 
         return [ 
          proxySnippet, 
          connect.static('.tmp'), 
          connect().use(
           '/bower_components', 
           connect.static('./bower_components') 
          ), 
          connect.static(appConfig.app), 
         ]; 
        } 
       } 
      }, 
      main: { 
       options: { 
        open: true, 
        base: '<%= homer.main %>' 
       } 
      } 
     } 

... 

    grunt.registerTask('live', [ 
     'clean:server', 
     'copy:styles', 
     'configureProxies', 
     'connect:livereload', 
     'watch' 
    ]); 

    grunt.registerTask('server', [ 
     'build', 
     'connect:main:keepalive' 
    ]); 
+0

不確定,但是您是否嘗試在已註冊的'live'任務中將連接目標添加到'configureProxies'中。像這樣:'configureProxies:connect:server'? – DavidDomain

+0

對不起,我找到了答案,並且在發佈答案時沒有看到您的回覆。是的,指定連接目標是問題。儘管由於某些原因,您的代碼段不能直接使用。相反,我使用'configureProxies:server'或(我實際上做了解決這個事情)完全刪除「靜態」目標,所以我可以使用''configureProxies',...'。 – Lindauson

+0

沒問題,至少你知道了。我不確定,但如果你可以的話,你應該接受你自己的答案。 ;-) – DavidDomain

回答

2

在configureProxies任務指定連接目標(在這種情況下, '服務器')。

grunt.registerTask('live', function (target) { 
    grunt.task.run([ 
    'clean:server', 
    'copy:styles', 
    'configureProxies:server', 
    'connect:livereload', 
    'watch' 
    ]); 
});