Gulp - connect proxy example

less than 1 minute read

It will avoid CORS problem during development.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Proxy = require('gulp-connect-proxy');
...
// A local web server for dev convenience
gulp.task("server", function() {
	connect.server({
	  root: "./dist",
	  port: 22532,
          middleware: function (connect, opt) {
	      opt.route = '/proxy';
	      var proxy = new Proxy(opt);
	      return [proxy];
	  }
	});
});

Example of calling API

1
2
3
4
return $http.get(
  'http://localhost:22532/proxy/external.host.com:8080/api/myservice/myid', 
  { params: { metricSelector: "hello2"} }
);

Gulp connect web site

https://www.npmjs.com/package/gulp-connect-proxy