Javascript 为什么grunt contrib connect的中间件选项的第三个参数未定义?
我使用的middleware选项模拟静态json数据,但中间件函数只有2个参数,第三个参数应该是一个数组,结果是未定义。 我的Gruntile作品:Javascript 为什么grunt contrib connect的中间件选项的第三个参数未定义?,javascript,node.js,gruntjs,livereload,grunt-contrib-connect,Javascript,Node.js,Gruntjs,Livereload,Grunt Contrib Connect,我使用的middleware选项模拟静态json数据,但中间件函数只有2个参数,第三个参数应该是一个数组,结果是未定义。 我的Gruntile作品: // The actual grunt server settings connect: { options: { port: 9000, livereload: 35729, // Change this to '0.0.0.0' to access the server from outs
// The actual grunt server settings
connect: {
options: {
port: 9000,
livereload: 35729,
// Change this to '0.0.0.0' to access the server from outside
hostname: '0.0.0.0'
},
server: {
options: {
open: 'http://localhost:9000',
base: [
'<%= yeoman.dist %>',
'<%= yeoman.tmp %>',
'<%= yeoman.app %>'
],
middleware: function(connect, options, middlewares) {
var bodyParser = require('body-parser');
// the middlewares is undefined,so here i encountered an error.
middlewares.unshift(
connect().use(bodyParser.urlencoded({
extended: false
})),
function(req, res, next) {
if (req.url !== '/hello/world') return next();
res.end('Hello, world from port #' + options.port + '!');
}
);
return middlewares;
}
}
},
test: {
options: {
port: 9001,
base: [
'<%= yeoman.tmp %>',
'test',
'<%= yeoman.app %>'
]
}
},
dist: {
options: {
open: true,
base: '<%= yeoman.dist %>',
livereload: false
}
}
},
问题并不是说中间软件没有定义。如果您有完整的堆栈跟踪,您会看到抛出的行实际上在您对connect.use的调用中 您无法取消将调用转移到Middleware阵列上以使用。相反,您应该只使用bodyParser生成的中间件,如下所示:
middlewares.unshift(
bodyParser.urlencoded({
extended: false
}),
function(req, res, next) {
if (req.url !== '/hello/world') return next();
res.end('Hello, world from port #' + options.port + '!');
}
);
您使用的是哪一版本的grunt contrib connect?@Interrobang通过键入npm view grunt contrib connect version,它显示0.10.1不工作。实际上,当我打印参数时,它只显示了2个参数。如何进行完整的堆栈跟踪?:在退出时打印堆栈跟踪,并显示警告或致命错误。我跟踪到源文件中,发现包已过时,即使版本是最新的。因此,我重新安装了包,它工作正常!
middlewares.unshift(
bodyParser.urlencoded({
extended: false
}),
function(req, res, next) {
if (req.url !== '/hello/world') return next();
res.end('Hello, world from port #' + options.port + '!');
}
);