2016-03-03 74 views
8

我使用grunt-connect-proxy「^ 0.2.0」代理我的angularjs應用程序的api。該項目從yeoman角發電機開始。Grunt連接代理:404沒有找到

我跟着指示here,但使用的代理時,我得到:

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

我的代理配置:

connect: { 
    options: { 
    port: 9000, 
    // Change this to '0.0.0.0' to access the server from outside. 
    hostname: 'localhost', 
    livereload: 35729 
    }, 
    proxies: [{ 
    context: '/api', // the context of the data service 
    host: 'localhost', // wherever the data service is running 
    port: 8080, // the port that the data service is running on 
    https: false, 
    xforward: false 
    }], 

我中間件:

livereload: { 
    options: { 
     open: true, 
     base: '', 
     middleware: function (connect, options) { 
     var middlewares = []; 

     middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest); 

     middlewares.push(connect.static('.tmp')); 
     middlewares.push(connect.static('test')); 
     middlewares.push(connect().use(
      '/bower_components', 
      connect.static('./bower_components') 
     )); 
     middlewares.push(connect.static(appConfig.app)); 

     return middlewares; 
     } 
    } 
    }, 

我的發球任務

grunt.registerTask('serve', 'Compile then start a connect web server', function (target) { 
    if (target === 'dist') { 
    return grunt.task.run(['build', 'connect:dist:keepalive']); 
    } 

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

編輯 本地主機:8080 /用戶返回當前通過郵遞員403,因此API運行。

+0

您是否嘗試使用服務器:{}之前的選項? –

+0

看看這個問題..也許它會幫助https://stackoverflow.com/questions/34321143/adding-grunt-connect-proxy-to-generator-angular-gruntfile-js – aprok

回答

0

沒有直接回答你的問題,但提供了一個解決方案(因爲沒有人其他人回答)...您是否嘗試過NPM模塊連接,modrewrite

這也正是你正在嘗試做的。

0

我遇到了同樣的問題,我解決了這個問題,併爲我工作。看看下面將我的代碼: -

connect: { 
      server: { 
       options: { 
        keepalive: true, 
        port: 8001, 
        protocol: 'http', 
        hostname: '*', 
        directory: 'dist', 
        open: { 
         target: 'http://localhost:8001/myDemo.html', 

        }, 
         middleware: function(connect, options, middlewares) { 

           middlewares.unshift(function(req, res, next) { 
            res.setHeader('Access-Control-Allow-Credentials', true); 
            res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
            res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); 
            **if (req.method.toUpperCase() == 'POST') req.method='GET';** 
            return next(); 
           }); 

           return middlewares; 
         } 

       } 
      } 
     }, 

看到(req.method.toUpperCase()== 'POST'),即如果恆星標線req.method = 'GET';我做了這個伎倆,它爲我工作。這個文章也對我有所幫助https://github.com/gruntjs/grunt-contrib-connect#middleware