为什么grunt contrib connect的中间件选项的第三个参数是未定义的
Why the third arguments of middlewares option of grunt-contrib-connect is undefined?
我使用grunt contrib connect的中间件选项来模拟静态json数据,但中间件函数只有两个参数,第三个参数本应是数组,但结果没有定义。我的文章:
// 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.
问题实际上并不在于middlewares
未定义。如果您有一个完整的堆栈跟踪,您会看到抛出的行实际上在对connect().use()
的调用中。
您不能将对use()
的调用取消转移到中间件阵列上。相反,您应该只使用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的中间件选项的第三个参数是未定义的
- 如何使用参数按顺序运行三个任务
- ruby Mustache.render没有像JS Mustache.render这样的部分的第三个参数
- 将变量传递给第三个参数的 GSAP TimeLineLite.to() 不起作用
- 如何创建和调用接受三个参数(页面长度)并计算块面积的 JavaScript 函数
- 第三个是bean.fire事件中的第三个参数
- showModalDialog第三个参数错误
- 将0分配给JavaScriptDate()对象的第三个参数将始终创建月末日期
- 这个dat.gui添加函数中第三个参数的含义是什么
- deviceready'中第三个参数的用途是什么;电话间隙中的听众
- 函数只看到三个参数
- 可以'在angularjs热键中找不到第三个参数的作用
- Jquery html传递一个值作为第三个参数
- 一个有三个参数的自执行匿名函数的意义是什么
- JSON.stringify第三个参数的行为
- 如何使用Bluebird.js nodeify将第三个参数传递给回调函数
- Javascript - Array.prototype.forEach() -是必需的三个参数
- app.get() Node.js 中的第三个参数与 Express
- 选择性地改变一个类的三个参数
- XMLHttpRequest.打开第三个参数false不给php mysql在iPad上的查询结果