我的应用程序(http://localhost:8099
)正在对https://api.parse.com/
执行一些CORS请求,我希望将这些请求代理到我本地的api http://localhost:8077
进行测试。这可以用grunt-connect-proxy
实现吗?
这是我的grunt connect-proxy配置,它的工作方式与我预期的不同。
connect: {
dev: {
options: {
port: 8099,
hostname: 'localhost',
base: 'build/',
livereload: false,
keepalive: true,
middleware: function (connect, options) {
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
// Include the proxy first
proxy,
// Serve static files.
connect.static('build/')
];
}
}
},
proxies: [
{
context: ['api/'], //same domain api requests, proxy works fine!
host: 'localhost',
port: 8077
},
{
context: ['api.parse.com/'], //cors, proxy is not working
host: 'localhost',
port: 8077,
changeOrigin: true
}]
}
→ grunt serve
Proxy created for: api/ to localhost:8077
Proxy created for: api.parse.com/ to localhost:8077
因此,代理is基本上适用于api/
请求(相同的域),但对于对api.parse.com/
的cors请求则被完全忽略。想法?
发布于 2015-04-02 16:55:10
当您向api.parse.com
发出请求时,浏览器将连接到实际的parse.com服务器。只有在向应用程序服务器发出请求时,grunt connect-proxy才会出现,在您的示例中,应用程序服务器是localhost:8099。
其他所有的localhost:8099对于你的应用程序来说都是远程的/跨域的(甚至是localhost:8077),你可以使用grunt connect -proxy在服务器端连接到这些服务器,而在客户端,你仍然会向你自己的服务器发出请求。
使用上下文配置代理时要连接到哪台服务器。
proxies: [
{
context: ['api/'],
host: 'localhost',
port: 8077
},
{
context: ['parse/'],
host: 'api.parse.com'
}]
因此,考虑到上面的配置
localhost:8099/api
--> Will to localhost:8077
和
localhost:8099/parse
-->将转到api.parse.com
https://stackoverflow.com/questions/29387904
复制相似问题