LoginSignup
0
0

More than 5 years have passed since last update.

node-http-proxy-agentを使ってgulp-webserverのproxiesでProxyを越えて接続する

Last updated at Posted at 2016-12-18

下記みたいに、gulp-webserverのproxiesオプションを使うと、
とある/hogeというリクエストを別サーバのhttp://fuga.com/fuga にプロキシすることができます。

gulpfile.js
var gulp = require('gulp');
var webserver = require('gulp-webserver');

gulp.task('webserver', function() {
  gulp.src('app')
  .pipe(webserver({
    proxies: [ {
      source: '/hoge',
      target: 'http://fuga.com/fuga'
    } ]
  }));
});

ここで、このhttp://fuga.com/fuga がProxyを越えて接続しないといけない場合についての接続方法について紹介します。
(例えば、APIサーバがAWS上にあるとか)

node-http-proxy-agent

node-http-proxy-agentというnpmモジュールを使うことでProxyを越えてgulp-webserverのproxies先に接続することができます。

gulpfile.js
var gulp = require('gulp');
var webserver = require('gulp-webserver');
var HttpProxyAgent = require('http-proxy-agent');

gulp.task('webserver', function() {
  gulp.src('app')
  .pipe(webserver({
    proxies: [ {
      source: '/hoge',
      target: 'http://fuga.com/fuga',
      options: {
        agent: new HttpProxyAgent(process.env.https_proxy)
      }
    } ]
  }));
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0