我使用grunt-contrib-connect的中间件选项来模拟静态 json 数据,但是中间件函数只有 2 个参数,应该是数组的第三个参数结果是未定义的。我的 gruntfile 片段:
// 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
}
}
},
错误是:
Running "connect:server" (connect) task
Warning: Cannot read property 'unshift' of undefined Use --force to continue.
Aborted due to warnings.