我使用有角度的种子,我把它放在我的Gruntfile.js
中,因为我需要允许CORS进行开发。
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: '0.0.0.0',
livereload: 35729,
// protocol: 'https',
// remove next from params
},
livereload: {
options: {
open: true,
base: [
'.tmp',
'<%= yeoman.app %>'
],
middleware: function(connect, options, middlewares) {
var enableREST = function(req, res, next){
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
return next();
};
middlewares.unshift(enableREST);
}
}
},
test: {
options: {
port: 9001,
base: [
'.tmp',
'test',
'<%= yeoman.app %>'
]
}
},
dist: {
options: {
base: '<%= yeoman.dist %>'
}
}
},
但是当我运行grunt serve
时,我收到middlewares
未定义的错误。
$ grunt serve
...
Running "connect:livereload" (connect) task
Verifying property connect.livereload exists in config...OK
File: [no files]
Options: protocol="http", port=9000, hostname="0.0.0.0", base=[".tmp","app"], directory=null, keepalive=false, debug=false, livereload=35729, open, middleware=undefined
Warning: Cannot call method 'unshift' of undefined Use --force to continue.
Aborted due to warnings.
我尝试过设置这样的中间件:
middleware: function(connect, options) {
return [
function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
return next();
},
// add other middlewares here
connect.static(require('path').resolve('.'))
];
}
然后localhost:9000
返回Cannot GET /
。我有什么错误的建议吗?
答案 0 :(得分:1)
刚刚使用此same problem,这是由过时的套餐造成的。
如果您将grunt-contrib-connect
更新为最新版本(目前为0.8.0),则可以解决您的问题。您可能还需要更改package.json
中的版本号(如果有的话)。