如何使用grunt服务配置grunt-connect-proxy?

时间:2015-05-31 08:00:46

标签: node.js express proxy gruntjs grunt-connect-proxy

我设法配置grunt来提供我的应用程序,但由于它在localhost:9000上运行,我的api调用也转到端口9000,而我的api在端口3000,导致404错误。

经过一番研究,我已经决定使用grunt-connect-proxy将我的api调用代理到正确的端口。我一直在打破每一篇文章的墙壁,堆栈溢出问题和文档,但我似乎无法正确配置。请参阅下面的gruntfile。任何帮助都将得到我不懈的感激。

// Invoke 'strict' JavaScript mode
'use strict';

module.exports = function(grunt) {
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
    grunt.initConfig({
        less: {
            development: {
                options: {
                    compress: true,
                    yuicompress: true,
                    optimization: 2,
                    paths: ['public/styles/less']
                },
                files: {
                    "public/styles/css/main.css": "public/styles/less/main.less" // destination file and source file
                }
            }
        },
        watch: {
            styles: {
                files: ['public/styles/less/*.less'],
                tasks: ['less'],
                options: {
                    nospawn: true
                }
            }
        },
        connect: {
            server: {
                options: {
                    port: 8000,
                    base: 'public',
                    logger: 'dev',
                    hostname: 'localhost',
                    middleware: function (connect, options, defaultMiddleware) {
                        var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
                        return [
                            // Include the proxy first
                            proxy
                        ].concat(defaultMiddleware);
                    }
                },
                proxies: [
                    {
                        context: '/',
                        host: '127.0.0.1',
                        port: 3000,
                        https: false,
                        xforward: false,
                        headers: {
                            "x-custom-added-header": 'value'
                        },
                        hideHeaders: ['x-removed-header']
                    }
                ]
            },
            serve: {
                options:{
                    port: 9000,
                    hostname: "127.0.0.1",
                    middleware: function(connect, options) {
                    return [
                        require('grunt-contrib-livereload/lib/utils').livereloadSnippet,
                        connect.static(options.base[0])
                    ];
                }
            }
        }
    },
    open: {
      serve: {
        path: 'http://localhost:<%= connect.serve.options.port%>/public'
      }
    },
    regarde: {
        serve: {
            files:['public/index.html','public/script/*.js','public/script/**/*.js','public/styles/**/*.css','public/styles/less/*.less','public/views/*.html'],
            tasks: ['livereload']
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-less');
//grunt.loadNpmTasks('grunt-contrib-clean');
//grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-connect-proxy');

grunt.registerTask('serve',['less','livereload-start','connect:serve','open:serve','regarde:serve']);
grunt.registerTask('server', function (target) {
    grunt.task.run([
        //'clean:server',
        //'compass:server',
        'configureProxies:server',
        'connect:server',
        'watch'
    ]);
});

};

0 个答案:

没有答案