我目前有一个使用yeoman配置的简单角度应用程序,我尝试的唯一自定义是使用grunt-connect-proxy。所以我按照说明修改了我的Gruntfile:
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
module.exports = function (grunt) {
...
grunt.initConfig({
...
connect: {
...
server: {
proxies: [
{
context: '/api',
host: 'somevirtualhost',
port: 8080,
https: false,
changeOrigin: true
}
]
},
livereload: {
options: {
open: true,
middleware: function (connect) {
return [
proxySnippet,
...
];
}
}
},
...
},
});
...
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
...
grunt.task.run([
...
'configureProxies:server',
...
]);
});
}
在控制器中我尝试了以下内容:
var Versions = $resource('/api/version/:versId', { versId: '@id' });
var version = Versions.get({ versId: 'Ultra' }, function () {
console.log('here I am');
});
当我运行grunt服务时,我可以看到代理已创建:
Running "configureProxies:server" (configureProxies) task
Proxy created for: /api to somevirtualhost:8080
但是在网页的控制台中我得到了:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:9000/api/version/Ultra
有什么想法吗?
答案 0 :(得分:1)
我在这里找到了一些线索:https://github.com/drewzboto/grunt-connect-proxy/issues/69 我不明白一切,但现在我在我的代理中添加了header选项:
proxies: [
{
context: '/api',
host: 'somevirtualhost',
port: 8080,
https: false,
changeOrigin: true,
headers: {
'host': 'somevirtualhost'
},
}
]
似乎有效