Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp + BrowserSync, serve at path

I'm using Gulp and BrowserSync to serve my webapp at localhost:9000.
How can I serve the webapp at localhost:9000/some/multi/level/path instead?

like image 967
Martin Stålberg Avatar asked May 21 '15 10:05

Martin Stålberg


People also ask

How do I disable Browsersync?

Just Press ctrl + c You will asked to press Y / N And if you press Y then the browser sync will stop along with other command line tools.

What is Browsersync io?

Browsersync is a module for Node. js, a platform for fast network applications.


3 Answers

Browsersync's static server can be configured to serve pages from any arbitrary subpath. When initializing Browsersync's static server, add a route definition where the key is the url fragment to match and the value is the directory to be served (path should be relative to the current working directory).

Try something like this:

var gulp = require('gulp');
var browsersync = require('browser-sync').create();

gulp.task('watch', function() {
  browsersync.init({
    files: './*.html',
    startPath: '/some/multi/level/path',
    server: {
      baseDir: '-',
      routes: {
        '/some/multi/level/path': '.'
      }
    }
  });
});

Running gulp watch will start Browsersync and open a page with the contents of ./ displayed at the url http://localhost:3000/some/multi/level/path.

baseDir must be set to a non-empty string and doesn't need to be a valid path. Falsey values (null, false and empty strings) won't work.

The snippet above is a working gulpfile and was tested against Browsersync v2.18.5 and gulp v3.9.1. Here's the complete gist.

like image 136
joemaller Avatar answered Sep 24 '22 23:09

joemaller


In your browsersync options pass in startPath to have it start on a different URL that '/'.

This doesn't change what your server is serving only the path BrowserSync will start with.

http://www.browsersync.io/docs/options/#option-startPath

like image 33
Landstander Avatar answered Sep 20 '22 23:09

Landstander


Seems the option isn't available. startPath will only change which URL is opened by browsersync.

I finally came up with a simple solution :

Just create a symbolic link from some/multi/level/path directory to your app files. Then going to localhost:9000/some/multi/level/path will provide the same files as going to localhost:9000

like image 29
Michael Laffargue Avatar answered Sep 22 '22 23:09

Michael Laffargue