我在package.json中安装了以下grunt插件:
"grunt-connect-proxy": "^0.2.0",
"grunt-contrib-connect": "^0.11.2",
"grunt-contrib-watch": "^0.6.1",
"jit-grunt": "^0.9.1",
"load-grunt-config": "^0.17.2",
"load-grunt-tasks": "^3.2.0"
以及以下grunt文件:
'use strict';
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-connect-proxy');
grunt.loadNpmTasks('grunt-contrib-connect');
require('time-grunt')(grunt);
require('load-grunt-config')(grunt, {
loadGruntTasks: {
pattern: ['grunt-*'],
config: require('./package.json')
},
jitGrunt: true
});
require('jit-grunt')(grunt, {
pluginsRoot: '/grunt',
'grunt-connect-proxy': 'grunt-connect-proxy',
serve: 'tasks/serve.js'
});
};
以及以下connect-contrib-connect任务:
'use strict';
// The node js test server for serving client files
module.exports = function(grunt) {
return {
options: {
port: 9000,
hostname: 'localhost'
},
proxies: [{
context: '/api', // the api pattern you need to proxy(i am going to proxy all the requests that are having a path as /api)
host: 'localhost', // the backend server host ip
port: 8080 // the backend server port
}],
livereload: {
options: {
open: true,
middleware: function (connect, options) {
grunt.log.write(JSON.stringify(connect)); // connect is undefined
grunt.log.write(JSON.stringify(options)); // has values
// custom middleware
return [];
}
}
},
dist: {
options: {
open: true,
base: 'dist'
}
}
};
}
问题是connect对象没有传递给中间件签名。这导致我的服务器内容无法正确显示。我正在寻找一些像
这样的静态内容connect().use('/bower_components', connect.static('/bower_components'))
并将其推送到中间件,与grunt-contrib-connect doc上的中间件示例并不太相似:https://github.com/gruntjs/grunt-contrib-connect#middleware
有人有任何想法吗?