2016-03-03 59 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