Grunt连接不以错误开始“根path必须是一个string”

我试图设置我的Gruntfile使用grunt连接npm模块。 我尝试使用grunt clean server命令启动服务器时遇到问题。 它的错误与行:

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

我真的不确定我搞错了什么configuration,可以使用另一套眼睛。 这是我的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但我不知道为什么。

任何帮助表示赞赏。

那么,我不明白怎么样,但似乎我的middleware函数中的options.base正在成为一个数组,其第一个值是我的BUILD目录。

因此,我的middlewarefunction的以下片段工作:

 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] 。 如果有人解释为什么是这样的话,那将是非常感激。

比赛迟到了,但在更新版本的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]安装第一个基础要好。

原因是options.base是一个数组。 options.base [0]引用数组中第一个(在这个例子中是唯一的)项(这是一个代表根path的string)。