gpt4 book ai didi

node.js - Grunt 连接未启动,错误为 "root path must be a string"

转载 作者:搜寻专家 更新时间:2023-10-31 23:05:08 26 4
gpt4 key购买 nike

我正在尝试设置我的 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 但我不知道为什么会这样。

感谢任何帮助。

最佳答案

好吧,我不明白怎么做,但我的 middleware 函数中的 options.base 似乎变成了一个数组,第一个值是我的 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]。如果有人能解释为什么会这样,我们将不胜感激。

关于node.js - Grunt 连接未启动,错误为 "root path must be a string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27176920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com