Search code examples
javascriptnode.jsgruntjsgrunt-connect-proxygrunt-connect

How do I resolve 404 error from misconfigured grunt-connect-proxy settings in Gruntfile.js?


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'
    ]);

Solution

  • 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'
        ]);
    });