简体   繁体   English

使用gulp-connect插件在gulp中配置中间件

[英]Configuring middleware in gulp using gulp-connect plugin

My project's back-end part runs on http://localhost:8080 and front-end is running on gulp-connect server on http://localhost:8811 .While running on chrome, whenever a REST api call is made, chrome generates this error message- 我项目的后端部分运行在http://localhost:8080而前端运行在http://localhost:8811上的gulp-connect服务器上。当在chrome上运行时,每当进行REST api调用时,chrome就会生成此错误消息-

No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头

Can this error be removed using proxy configuration in gulp-connect's middleware options ? 可以使用gulp-connect的中间件选项中的proxy配置来消除此错误吗? if yes then I want to know how. 如果是,那么我想知道如何。 I tried setting a response header 'allow-origin' to ' http://localhost:8811 ' from back-end and it worked but i want to know if gulp can help remove that error. 我尝试将后端的响应标头'allow-origin'设置为' http:// localhost:8811 ',它可以工作,但是我想知道gulp是否可以帮助消除该错误。

Following is snippet from my gulpfile.js 以下是我的gulpfile.js代码段

gulp.task('webserver',function(){
    gulpWebServer.server({
        root : ['.'],
        port : 8811,
        host : 'gulp_dev',
        livereload : true,
        middleware: function(connect, opt) {
            return [
                proxy({ changeOrigin: true,target: 'http://localhost:8080'})
            ];
        }
    });
});

and service is as follows : service如下:

angular.module('RestAssignment').service('EmployeeService',["$resource",function($resource){
    return $resource('',{},{
        fetchEmployeeById :{
            url:'http://localhost:8080/rest/abc/getEmployeeById/2',
            method:'GET'
        },
        fetchEmployeeList : {
            url:'http://localhost:8080/rest/abc/getAllEmployees',
            method:'GET',
            isArray : true
        }
    },{});
}]);

FIXED: No 'Access-Control-Allow-Origin' header is present on the requested resource 固定:请求的资源上不存在“ Access-Control-Allow-Origin”标头

install cors package: 安装cors软件包:

npm install --save-dev cors

then add it as middleware to connect: 然后将其添加为中间件以进行连接:

var gulp = require('gulp');
var connect = require('gulp-connect');
var cors = require('cors');

gulp.task('connect', function() {
  connect.server({
    root: 'app',
    middleware: function() {
        return [cors()];
    }
  });
});

Reference: here 参考: 这里

The problem was that I was specifying complete URL (even with protocol and port) in $resource in EmployeeService which i think was nullifying the effect of proxy and second part of the problem was that i have not specified any path in proxy( path , options) function and because of this all requests were getting proxied but I wanted only REST calls to be proxied as i was getting 'No Access-Control-Allow-Origin header' while making REST API calls. 问题是我在EmployeeService $resource中指定了完整的URL(甚至带有协议和端口),我认为这使代理的作用无效,问题的第二部分是我没有在proxy( pathoptions)指定任何路径options)功能,因此,所有请求都被代理,但是我只希望代理REST调用,因为在进行REST API调用时,我收到“ No Access-Control-Allow-Origin标头” so I changed gulpfile.js to the following : 所以我将gulpfile.js更改为以下内容:

gulp.task('webserver',function(){
    gulpWebServer.server({
        root : ['.'],
        port : 8811,
        host : 'gulp_dev',
        livereload : true,
        middleware: function(connect, opt) {
            return [
                return proxy('/rest/**', { target: 'http://localhost:8080', changeOrigin : true});
            ];
        }
    });
});

and EmoloyeeService to : EmoloyeeService到:

angular.module('LNAssignment').service('EmployeeService',["$resource",function($resource){
    return $resource('',{},{
        fetchEmployeeById :{
            url:'/rest/abc/getEmployeeById/2',
            method:'GET'
        },
        fetchEmployeeList : {
            url:'/rest/abc/getAllEmployees',
            method:'GET',
            isArray : true
        }
    },{});
}]);

Now the middleware proxy works perfectly without any CORS related error message. 现在,中间件代理可以完美运行,而没有任何与CORS相关的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM