How to configure port in browser-sync

2019-03-25 14:56发布

问题:

I have a gulp task running with browser-sync,by default its running on port 3000 of node.js server.I want to change the default port to any other port like 3010.

    var gulp = require('gulp'), 
    connect = require('gulp-connect'),          
    browserSync = require('browser-sync');    

    gulp.task('serve', [], function() {
    browserSync(
      {
        server: "../ProviderPortal"
       });
   });
   /*** 8. GULP TASKS **********/
   gulp.task('default', ['serve']);

I am using:

browser-sync version-2.6.1

I tried configuring the gulp task like:

gulp.task('serve', [], function() {
    browserSync(
    {
        ui: {
       port: 8080
       },
        server: "../ProviderPortal"
    });
});

But it didnot work.

回答1:

Answer based on the documentation links (link1, link2).

You are using browser-sync version 2.0+, they have a different recommended syntax. Using that syntax your code could be like this:

// require the module as normal
var bs = require("browser-sync").create();

....

gulp.task('serve', [], function() {
  // .init starts the server
  bs.init({
    server: "./app",
    port: 3010
  });
});

You specify required port directly in configuration object.