I would like to open a browser window when my web-server is running, at the moment I am using this, but I am not able to open the browser, any idea what is wrong in my code?
var gulp = require('gulp');
var connect = require('gulp-connect');
gulp.task('webserver', function () {
connect.server({
root: "../../../www/",
livereload: true,
open: {
browser: 'chrome', // if not working OS X browser: 'Google Chrome'
url: 'http://localhost:8080/site/a/index.html?dev'
}
});
});
In my opinion, instead of a specific gulp task, you could just use a native node module, "opn" https://www.npmjs.com/package/opn
npm install --save-dev opn
Then in whatever callback your server module uses:
require("opn")("http://localhost:8080/site/a/index.html?dev",
{app: ['google chrome', '--incognito']})
gulp-connect doesn't look like it provides a callback, but you can probably just run the open task serially, or after a short wait. Other competitors to gulp-connect do provide a callback, which allows nice things like passing port/ip etc. dynamically to opn, allowing you to further configure what happens (browsersync, for instance, dynamically checks and uses an free port, and then passes along information about which port it used, which allows opn to open the correct local port automatically even if it changes from time to time. ).