Gulp Browsersync and http-proxy-middleware offline

Is Browsersync used with http-proxy-middleware if I am a proxy server on a local server?

I have an angular application deployed to localhost:3000creating requests for an api server deployed to localhost:8080. HTTP requests to the api server are proxied by browsers http-proxy-middleware. Everything works fine if I have an internet connection, but if I go offline, I will get the following error in the gulp console:

[HPM] Proxy error: ENOENT localhost:8080/data/pet

And on the browser console:

Failed to load resource: the server responded with a status of `500 (Internal Server Error)`

I know that the error is not on the server, because I can still achieve localhost:8080/data/petusing the browser.

Since the api server is deployed locally, it seems strange that I need an internet connection for the http request proxy.

The angular app with all its configuration was created using generator-gulp-angular

This is my gulp server configuration:

'use strict';

var path = require('path');
var gulp = require('gulp');
var conf = require('./conf');

var browserSync = require('browser-sync');
var browserSyncSpa = require('browser-sync-spa');

var util = require('util');

var proxyMiddleware = require('http-proxy-middleware');

function browserSyncInit(baseDir, browser) {
  browser = browser === undefined ? 'default' : browser;

  var routes = null;
  if (baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
    routes = {
      '/bower_components': 'bower_components'
    };
  }

  var server = {
    baseDir: baseDir,
    routes: routes
  };


  server.middleware = proxyMiddleware('/data',
    {
      target: 'http://localhost:8080'
    });

  browserSync.instance = browserSync.init({
    startPath: '/',
    server: server,
    browser: browser,
    online: false
  });
}

browserSync.use(browserSyncSpa({
  selector: '[ng-app]'// Only needed for angular apps
}));

gulp.task('serve', ['watch'], function () {
  browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src]);
});

gulp.task('serve:dist', ['build'], function () {
  browserSyncInit(conf.paths.dist);
});

gulp.task('serve:e2e', ['inject'], function () {
  browserSyncInit([conf.paths.tmp + '/serve', conf.paths.src], []);
});

gulp.task('serve:e2e-dist', ['build'], function () {
  browserSyncInit(conf.paths.dist, []);
});

And this is my .json package:

{
  "name": "petStore",
  "version": "0.0.0",
  "dependencies": {},
  "scripts": {
    "test": "gulp test"
  },
  "devDependencies": {
    "gulp": "~3.9.0",
    "gulp-autoprefixer": "~2.3.1",
    "gulp-angular-templatecache": "~1.6.0",
    "del": "~1.2.0",
    "lodash": "~3.9.3",
    "gulp-csso": "~1.0.0",
    "gulp-filter": "~2.0.2",
    "gulp-flatten": "~0.0.4",
    "gulp-jshint": "~1.11.0",
    "gulp-load-plugins": "~0.10.0",
    "gulp-size": "~1.2.1",
    "gulp-uglify": "~1.2.0",
    "gulp-useref": "~1.2.0",
    "gulp-util": "~3.0.5",
    "gulp-ng-annotate": "~1.0.0",
    "gulp-replace": "~0.5.3",
    "gulp-rename": "~1.2.2",
    "gulp-rev": "~5.0.0",
    "gulp-rev-replace": "~0.4.2",
    "gulp-minify-html": "~1.0.3",
    "gulp-inject": "~1.3.1",
    "gulp-protractor": "~1.0.0",
    "gulp-sourcemaps": "~1.5.2",
    "gulp-less": "~3.0.3",
    "gulp-angular-filesort": "~1.1.1",
    "main-bower-files": "~2.8.0",
    "merge-stream": "~0.1.7",
    "jshint-stylish": "~2.0.0",
    "wiredep": "~2.2.2",
    "karma": "~0.12.36",
    "karma-jasmine": "~0.3.5",
    "karma-phantomjs-launcher": "~0.2.0",
    "karma-angular-filesort": "~0.1.0",
    "karma-ng-html2js-preprocessor": "~0.1.2",
    "concat-stream": "~1.5.0",
    "require-dir": "~0.3.0",
    "browser-sync": "~2.7.12",
    "browser-sync-spa": "~1.0.2",
    "http-proxy-middleware": "~0.2.0",
    "chalk": "~1.0.0",
    "uglify-save-license": "~0.4.1",
    "wrench": "~1.5.8"
  },
  "engines": {
    "node": ">=0.10.0"
  }
}

And this is the http call:

  $http.get("/data/pet").then(function (result) {
        vm.petName = result.data.name;
      });

And bower.json

{
  "name": "petStore",
  "version": "0.0.0",
  "dependencies": {
    "angular-resource": "~1.4.0",
    "angular-ui-router": "~0.2.15",
    "bootstrap": "~3.3.4",
    "angular-bootstrap": "~0.13.0",
    "malarkey": "yuanqing/malarkey#~1.3.0",
    "toastr": "~2.1.1",
    "moment": "~2.10.3",
    "animate.css": "~3.3.0",
    "angular": "~1.4.0"
  },
  "devDependencies": {
    "angular-mocks": "~1.4.0"
  },
  "resolutions": {
    "angular": "~1.4.0"
  }
}
+4
source share
2 answers

Just a few tests, and I can confirm this problem. Run library tests using "npm test". It will install the actual servers to run some tests.

When online, all tests are successful. But when running tests in standalone mode, it gives the same error message.

Actual proxy functionality is provided by node-http-proxy .

This question was sent to: https://github.com/nodejitsu/node-http-proxy/issues/835

, http-proxy .

( : http-proxy-middleware.)

+3

, core browsersync . .

: https://browsersync.io/docs/options/#option-online

server.js, :

browserSync.instance = browserSync.init({ startPath: '/', server: server, browser: browser, online: false });

0

All Articles