2

Background:

I'm trying to connect my grunt server instance, to my API service running on the same machine at localhost:8080/api/.

Currently using grunt-connect-proxy to achieve this.

Problem/Question:

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

Is there an error with my config (below) that is preventing /api request from redirecting to the proxy server at localhost:8080?

My Settings (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'
    ]);
Lindauson
  • 2,963
  • 1
  • 30
  • 33
  • Not sure, but did you try to add the connect target to your `configureProxies` in the registered `live` task. Like so: `configureProxies:connect:server` ? – DavidDomain Jun 27 '15 at 19:21
  • I'm sorry, I figured this out and didn't see your response when I posted an answer. Yes, specifying the connect target was the problem. Though for some reason your snippet doesn't work directly. Instead I use `configureProxies:server` or (what I actually did to solve this) remove the "static" target entirely so I could just use `'configureProxies', ... `. – Lindauson Jun 27 '15 at 22:40
  • No problem, at least you figured it out. I am not sure, but if you can you should accept your own answer. ;-) – DavidDomain Jun 27 '15 at 22:44

1 Answers1

2

Specify the connect target ('server' in this case) in the configureProxies task.

grunt.registerTask('live', function (target) {
    grunt.task.run([
    'clean:server',
    'copy:styles',
    'configureProxies:server',
    'connect:livereload',
    'watch'
    ]);
});
Lindauson
  • 2,963
  • 1
  • 30
  • 33