grunt 请求的代理仅适用于 HTTP(而非 HTTPS)
Proxy for requests with grunt only works with HTTP (and not HTTPS)
现在我们的数据源在 manifest.json
中配置如下(顺便说一句,这会导致 CORS 错误):
"dataSources": {
"contractsRemote": {
"uri": "https://myCompany:8443/sap/opu/odata/SAP/Z_TEST_SRV/",
"type": "OData",
},
"userInfoRemote": {
"uri": "https://myCompany:8443/sap/bc/ui2/start_up",
"type": "JSON"
}
}
如果我们部署我们的应用程序(通过 /UI5/UI5_REPOSITORY_LOAD
上传),我们必须将 URI 更改为此
"dataSources": {
"contractsRemote": {
"uri": "/sap/opu/odata/SAP/Z_TEST_SRV/",
"type": "OData",
},
"userInfoRemote": {
"uri": "/sap/bc/ui2/start_up",
"type": "JSON"
}
}
如果我们只在本地开发环境中使用相对 URI(来自第二个片段),会容易得多。因此,为了同时解决 CORS 和 URI 问题,我想设置一个 grunt 任务(警告,我以前从未这样做过)代理对 https://myCompany:8443/path
.
的相关请求
我从一些 SAP github 存储库中获取示例 Gruntfile.js
并为代理添加了一些行,并且它有效,但只能通过 HTTP.如果我将代理端口更改为 8443
并将 https 设置为 true
,我会收到以下错误
> Proxy error: ECONNRESET
这是我的Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
dir: {
webapp: 'webapp',
dist: 'dist',
bower_components: 'bower_components'
},
connect: {
options: {
port: 8000,
base: 'public',
hostname: 'localhost',
middleware: function(connect, options, defaultMiddleware) {
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
proxy
].concat(defaultMiddleware);
}
},
proxies: [{
context: '/sap',
host: 'myCompany',
port: 8443,
https: true
}],
src: {},
dist: {}
},
openui5_connect: {
options: {
resources: [
'<%= dir.bower_components %>/openui5-sap.ui.core/resources',
'<%= dir.bower_components %>/openui5-sap.m/resources',
'<%= dir.bower_components %>/openui5-themelib_sap_bluecrystal/resources'
]
},
src: {
options: {
appresources: '<%= dir.webapp %>'
}
},
dist: {
options: {
appresources: '<%= dir.dist %>'
}
}
},
openui5_preload: {
component: {
options: {
resources: {
cwd: '<%= dir.webapp %>',
prefix: 'todo'
},
dest: '<%= dir.dist %>'
},
components: true
}
},
clean: {
dist: '<%= dir.dist %>/'
},
copy: {
dist: {
files: [{
expand: true,
cwd: '<%= dir.webapp %>',
src: [
'**',
'!test/**'
],
dest: '<%= dir.dist %>'
}]
}
},
eslint: {
webapp: ['<%= dir.webapp %>']
}
});
grunt.loadNpmTasks('grunt-connect-proxy');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-openui5');
grunt.loadNpmTasks('grunt-eslint');
grunt.registerTask('serve', function(target) {
grunt.task.run([
'configureProxies',
'openui5_connect:' + (target || 'src') + ':keepalive'
]);
});
grunt.registerTask('lint', ['eslint']);
grunt.registerTask('build', ['openui5_preload', 'copy']);
grunt.registerTask('default', [
//'lint',
'clean',
'build',
'serve:dist'
]);
};
Probably you’re already using grunt to serve your local frontend code. Everything is fine, but if you’re developing your backend with something different than JavaScript (Being a Java developer I heard that might happen), you will have problems accessing this backend while running grunt server.
grunt-connect-proxy 有一个 grunt 模块可以帮助您。它基本上将匹配给定 URL 的请求委托给您选择的不同后端。不幸的是,如果您不了解连接中间件的概念,我发现配置起来相当困难。
基本上你只需要在你的 Gruntfile.js 文件中添加两件事:
首先将连接服务器配置添加到 grunt.initConfig 内的配置 JSON 中。此示例将所有请求委托给 http://localhost:8000/services to http://localhost:8090/services——请记住,grunt 服务器在端口 8000 上为 运行,而后端在端口 8090 上:
我终于让它工作了。
首先,本地主机也必须通过 https 提供服务才能访问 https API。
其次,代理需要正确的协议 (https:
),但同时 https 必须设置为 false
。我猜是有原因的。
connect: {
options: {
base: 'public',
port: '443',
hostname: 'localhost',
protocol: 'https',
open: true,
livereload: true,
middleware: function (connect, options, defaultMiddleware) {
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
proxy
].concat(defaultMiddleware);
}
},
proxies: [
{
context: '/sap',
host: 'mySapHost',
port: '443',
https: false,
protocol: 'https:'
}
],
},
现在我们的数据源在 manifest.json
中配置如下(顺便说一句,这会导致 CORS 错误):
"dataSources": {
"contractsRemote": {
"uri": "https://myCompany:8443/sap/opu/odata/SAP/Z_TEST_SRV/",
"type": "OData",
},
"userInfoRemote": {
"uri": "https://myCompany:8443/sap/bc/ui2/start_up",
"type": "JSON"
}
}
如果我们部署我们的应用程序(通过 /UI5/UI5_REPOSITORY_LOAD
上传),我们必须将 URI 更改为此
"dataSources": {
"contractsRemote": {
"uri": "/sap/opu/odata/SAP/Z_TEST_SRV/",
"type": "OData",
},
"userInfoRemote": {
"uri": "/sap/bc/ui2/start_up",
"type": "JSON"
}
}
如果我们只在本地开发环境中使用相对 URI(来自第二个片段),会容易得多。因此,为了同时解决 CORS 和 URI 问题,我想设置一个 grunt 任务(警告,我以前从未这样做过)代理对 https://myCompany:8443/path
.
我从一些 SAP github 存储库中获取示例 Gruntfile.js
并为代理添加了一些行,并且它有效,但只能通过 HTTP.如果我将代理端口更改为 8443
并将 https 设置为 true
,我会收到以下错误
> Proxy error: ECONNRESET
这是我的Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
dir: {
webapp: 'webapp',
dist: 'dist',
bower_components: 'bower_components'
},
connect: {
options: {
port: 8000,
base: 'public',
hostname: 'localhost',
middleware: function(connect, options, defaultMiddleware) {
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
proxy
].concat(defaultMiddleware);
}
},
proxies: [{
context: '/sap',
host: 'myCompany',
port: 8443,
https: true
}],
src: {},
dist: {}
},
openui5_connect: {
options: {
resources: [
'<%= dir.bower_components %>/openui5-sap.ui.core/resources',
'<%= dir.bower_components %>/openui5-sap.m/resources',
'<%= dir.bower_components %>/openui5-themelib_sap_bluecrystal/resources'
]
},
src: {
options: {
appresources: '<%= dir.webapp %>'
}
},
dist: {
options: {
appresources: '<%= dir.dist %>'
}
}
},
openui5_preload: {
component: {
options: {
resources: {
cwd: '<%= dir.webapp %>',
prefix: 'todo'
},
dest: '<%= dir.dist %>'
},
components: true
}
},
clean: {
dist: '<%= dir.dist %>/'
},
copy: {
dist: {
files: [{
expand: true,
cwd: '<%= dir.webapp %>',
src: [
'**',
'!test/**'
],
dest: '<%= dir.dist %>'
}]
}
},
eslint: {
webapp: ['<%= dir.webapp %>']
}
});
grunt.loadNpmTasks('grunt-connect-proxy');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-openui5');
grunt.loadNpmTasks('grunt-eslint');
grunt.registerTask('serve', function(target) {
grunt.task.run([
'configureProxies',
'openui5_connect:' + (target || 'src') + ':keepalive'
]);
});
grunt.registerTask('lint', ['eslint']);
grunt.registerTask('build', ['openui5_preload', 'copy']);
grunt.registerTask('default', [
//'lint',
'clean',
'build',
'serve:dist'
]);
};
Probably you’re already using grunt to serve your local frontend code. Everything is fine, but if you’re developing your backend with something different than JavaScript (Being a Java developer I heard that might happen), you will have problems accessing this backend while running grunt server.
grunt-connect-proxy 有一个 grunt 模块可以帮助您。它基本上将匹配给定 URL 的请求委托给您选择的不同后端。不幸的是,如果您不了解连接中间件的概念,我发现配置起来相当困难。
基本上你只需要在你的 Gruntfile.js 文件中添加两件事:
首先将连接服务器配置添加到 grunt.initConfig 内的配置 JSON 中。此示例将所有请求委托给 http://localhost:8000/services to http://localhost:8090/services——请记住,grunt 服务器在端口 8000 上为 运行,而后端在端口 8090 上:
我终于让它工作了。
首先,本地主机也必须通过 https 提供服务才能访问 https API。
其次,代理需要正确的协议 (https:
),但同时 https 必须设置为 false
。我猜是有原因的。
connect: {
options: {
base: 'public',
port: '443',
hostname: 'localhost',
protocol: 'https',
open: true,
livereload: true,
middleware: function (connect, options, defaultMiddleware) {
var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
proxy
].concat(defaultMiddleware);
}
},
proxies: [
{
context: '/sap',
host: 'mySapHost',
port: '443',
https: false,
protocol: 'https:'
}
],
},