Search code examples
phpnode.jsgulphttp-proxybrowser-sync

Gulp PHP with BrowserSync and a proxy server throwing error


I had to redownload all of my dev tools after upgrading/reset on Windows 10. I can't seem to get my server working with gulp-connect-php, gulp-browser-sync, and http-proxy. The task (below) is actually from this answer: Gulp-webapp running BrowserSync and PHP.

This is on top of a generation of gulp-webapp from Yeoman. The regular "serve" works, but of course doesn't spin up PHP files. I had it working before the upgrade, but now the http-proxy errors out with an ECONNREFUSED when I try to bring up the URL. Note that it doesn't error out until I actually go to the URL, if the task is setup to not open the browser automatically, it won't error until I go to localhost:3000.

Any ideas the cause of this error?

Gulp Task

gulp.task('php-serve', ['styles', 'fonts'], () => {
  phpConnect.server({
    port: 9001,
    base: 'app',
    open: false
  });
  const proxy = httpProxy.createProxyServer({});
  browserSync({
    notify: false,
    open: false,
    port: 9000,
    server: {
      baseDir: ['.tmp', 'app'],
      routes: {
        '/bower_components': 'bower_components'
      },
      middleware: function (req, res, next) {
        var url = req.url;
        if (!url.match(/^\/(styles|fonts|bower_components)\//)) {
          proxy.web(req, res, { target: 'http://127.0.0.1:9001' });
        }
        else {
          next();
        }
      }
    }
  });

  gulp.watch([
    'app/*.html',
    'app/*.php',
    'app/scripts/**/*.js',
    'app/images/**/*',
    '.tmp/fonts/**/*'
  ]).on('change', reload);

  gulp.watch('app/styles/**/*.scss', ['styles']);
  gulp.watch('app/fonts/**/*', ['fonts']);
  gulp.watch('bower.json', ['wiredep', 'fonts']);
});

Gulp Run Task with Error

$ gulp php-serve
[07:30:09] Requiring external module babel-core/register
[07:30:11] Using gulpfile ~\...\gulpfile.babel.js
[07:30:11] Starting 'styles'...
[07:30:12] Starting 'fonts'...
[07:30:12] Finished 'styles' after 1.08 s
[07:30:12] Finished 'fonts' after 306 ms
[07:30:12] Starting 'php-serve'...
[07:30:12] Finished 'php-serve' after 183 ms

[BS] Access URLs:
----------------------------------
Local: http://localhost:9000
External: http://x.x.x.x:9000
----------------------------------
UI: http://localhost:3001
UI External: http://x.x.x.x:3001
----------------------------------
[BS] Serving files from: .tmp
[BS] Serving files from: app

C:\...\node_modules\http-proxy\lib\http-proxy\index.js:119

throw err;
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)

Solution

  • There was a bad install of http-proxy and/or gulp-php-connect. I uninstalled them, cleared npm cache, and did a fresh install. Everything worked fine after that.