Grunt连接没有以错误“root path必须是一个字符串”开头

时间:2014-11-27 18:23:58

标签: node.js gruntjs grunt-contrib-connect

我试图设置我的Gruntfile以使用grunt connect npm模块。尝试使用命令grunt clean server启动服务器时遇到问题。它出错了:

Warning: root path must be a string Use --force to continue.

我真的不确定我搞砸了什么配置,可以使用另一双眼睛。这是我的Gruntfile:

/* global module, conf */
var modRewrite = require('connect-modrewrite');
var mountFolder = function(connect, dir) {
    return connect.static(require('path').resolve(dir));
};
module.exports = function(grunt) {

    grunt.initConfig({
        copy: {
            base: {
                files: [
                    {src: "index.html", dest: "BUILD/index.html"},
                    {expand: true, src: "app/**", dest: "BUILD/"},
                    {expand: true, src: "assets/**", dest: "BUILD/"}
                ]
            }
        },

        connect: {
            proxies: [
                {
                    context: "/wwff",
                    host:  "localhost",
                    port: "8080"
                }
            ],

            /**
            * Task defines a server at 9000,
            * watching the BUILD directory
            */
            dev: {
                options: {
                    port: "9000",
                    hostname: '*',
                    base: "BUILD",
                    middleware: function(connect, options) {
                        var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

                        return [
                            // include the proxy first
                            proxySnippet,
                            modRewrite([
                                '!\\.html|\\.js|\\.swf|\\.json|\\.xml|\\.css|\\.png|\\.jpg|\\.gif|\\.ico|\\.aff|\\.msi|\\.zip|\\.dic$ /index.html [L]'
                            ]),
                            // serve static files
                            connect.static(options.base),
                            // make empty directories browsable
                            connect.directory(options.base)
                        ];
                    }
                }
            }
        },

        /*
        * This task watches the application and asset directories and
        * deploys any changes to the dev server
        */
        watch: {
            static: {
                files: [ "app/**/*.js", "app/**/*.html"],
                tasks: ["build"]
            }
        },

        clean: {
            build: ["BUILD/"],
            temp: ["tmp"]
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-connect-proxy');
    grunt.loadNpmTasks('grunt-contrib-clean');

    /*
    * Main BUILD Task
    */
    grunt.registerTask("build", "Copies app and asset files to the BUILD directory.", function() {
        grunt.task.run("copy:base");
    });

    grunt.registerTask("server", "Stand up a node server for development.", function() {
        grunt.task.run(["build", "configureProxies:dev", "connect:dev", "watch"]);
    });

    grunt.event.on('watch', function(action, filepath, target) { 
        grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
    });

};

当我使用--verbose标志时,我得到以下几行:

Verifying property connect.dev exists in config...OK
File: [no files]
Options: protocol="http", port="9000", hostname="*", base="BUILD", directory=null, keepalive=false, debug=false, livereload=false, open=false, useAvailablePort=false, onCreateServer=null, middleware=undefined

在我看来,问题是middlewareundefined,但我不知道为什么会这样。

感谢任何帮助。

3 个答案:

答案 0 :(得分:5)

好吧,我不明白怎么做,但似乎我的options.base函数中的middleware变成了一个数组,第一个值是我的BUILD目录。

因此,middleware函数的以下代码段可用:

middleware: function(connect, options) {
    var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

    return [
        // include the proxy first
        proxySnippet,
        modRewrite(['!\\.html|\\.js|\\.swf|\\.json|\\.xml|\\.css|\\.png|\\.jpg|\\.gif|\\.ico|\\.aff|\\.msi|\\.zip|\\.dic$ /index.html [L]']),
        // serve static files
        connect.static(options.base[0]),
        // make empty directories browsable
        connect.directory(options.base[0])
    ];
}

上述代码段中的重要部分是我的options.base现在是options.base[0]。如果有人解释为什么会出现这种情况,那将非常感激。

答案 1 :(得分:1)

比赛迟到但在较新版本的grunt-connect base中始终是一个数组。您可以使中间件与这两个版本兼容:

middleware: function (connect, options) {
    // Older versions of grunt-connect.
    if (!Array.isArray(options.base)) {
        options.base = [options.base];
    }

    var middlewares = [
        require('connect-livereload')()
    ];

    // Serve static files.
    options.base.forEach(function(base) {
        middlewares.push(connect.static(base));
    });

    return middlewares;
}

有关grunt-connect-proxy的示例,请参阅https://github.com/drewzboto/grunt-connect-proxy

上的文档

这比在options.base[0]安装第一个基地更好。

答案 2 :(得分:0)

原因是options.base是一个数组。 options.base [0]指的是数组中的第一个(在这种情况下是唯一的)项(表示根路径的字符串)。