gulp + browser-sync无法获取/错误

问题描述 投票:6回答:6

我正在学习前端构建系统目前gulp,我想使用brower-sync,问题是它不会在commad行中抛出错误,而是当它调出浏览器时它不会显示我的html文件而它会在浏览器窗口中说“无法获取/”错误。这是我的gulpfile.js代码

var gulp = require('gulp'),
   uglify = require('gulp-uglify'),
   compass= require('gulp-compass'),
   plumber = require('gulp-plumber'),
   autoprefixer = require('gulp-autoprefixer'),
   browserSync = require('browser-sync'),
   reload = browserSync.reload,
   rename = require('gulp-rename');


gulp.task('scripts', function() {
   gulp.src(['public/src/js/**/*.js', '!public/src/js/**/*.min.js'])
      .pipe(plumber())
      .pipe(rename({suffix: '.min'}))
      .pipe(uglify())
      .pipe(gulp.dest('public/src/js/'));
});

gulp.task('styles', function() {
   gulp.src('public/src/scss/main.scss')
      .pipe(plumber())
      .pipe(compass({
          config_file: './config.rb',
          css: './public/src/css/',
          sass: './public/src/scss/'
      }))
     .pipe(autoprefixer('last 2 versions'))
     .pipe(gulp.dest('public/src/css/'))
     .pipe(reload({stream:true}));
});


gulp.task('html', function() {
  gulp.src('public/**/*.html');
});  

gulp.task('browser-sync', function() {
    browserSync({
      server: {
         baseDir: "./public/"
      }
   });
});

gulp.task('watch', function() {
   gulp.watch('public/src/js/**/*.js', ['scripts']);
   gulp.watch('public/src/scss/**/*.scss', ['styles']);
   gulp.watch('public/**/*.html', ['html']);
});

gulp.task('default', ['scripts', 'styles', 'html', 'browser-sync', 'watch']);

我正在使用Windows和git bash,版本是"browser-sync": "^2.12.5"所以问题是什么,并尝试为我解释,以便从中获取一些东西。

gulp browser-sync gulp-browser-sync
6个回答
10
投票

你的index.html文件夹中是否有./public/文件?如果没有,您需要告诉browserSync您的起始页是什么。您还可以使用browserSync来显示基本目录的列表,请参阅下面的更新。

您也可以尝试使用没有前导点的public

编辑:startPath配置指令似乎不起作用,改为使用index

...
gulp.task('browser-sync', function() {
   browserSync({
     server: {
        baseDir: "public/",
        index: "my-start-page.html"
     }
   });
});
...

更新:实际上您可以使用browserSync获取目录列表。这样它会显示public中的文件列表,而不是Can not Get错误

...
gulp.task('browser-sync', function() {
   browserSync({
     server: {
        baseDir: "public/",
        directory: true
     }
   });
});
...

3
投票

当index.html文件位于同一文件夹中时,我得到了这个工作:

    browserSync.init({
        server: {
            baseDir: "./"
        }
    });

3
投票
gulp.task('browser-sync', function() {
    browserSync({

      server: {
            baseDir: "./"
            },

            port: 8080,
            open: true,
            notify: false
    });
});

0
投票

我通过始终在代码中指向.html文件,或者通过以和index.html结尾的文件夹结构解决了浏览器同步中的“无法获取”错误

<a href="/about">About</a> <!-- Cannot GET error-->
<a href="/about">About</a> <!-- same code as above, but works if your site structure is:  ./about/index.html-->
<a href="/about.html">About</a> <!-- OK -->

我的Gulp文件供参考(使用浏览器与jekyll同步,所以一切都在./_site文件夹中,gulp文件在./中)

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

gulp.task('build', shell.task(['jekyll build --watch']));

// serving blog with Browsersync
gulp.task('serve', function () {
    browserSync.init(
        {
            server: {baseDir: '_site/'}
        }
    );

    // Reloads page when some of the already built files changed:
    gulp.watch('_site/**/*.*').on('change', browserSync.reload);
});

gulp.task('default', ['build', 'serve']);

希望能帮助到你。

米开朗基罗


0
投票

也许你在index.html没有baseDir: '_site/'?为了在Web浏览器中查看静态Web页面而不是恼人的消息,您必须将文件your_file.html重命名为index.html。这将解决无法GET /问题。


0
投票
gulp.task('serve',['sass'], function() {
    bs.init({
        server: "./app",
        startPath: "/index.html", // After it browser running
        browser: 'chrome',
        host: 'localhost',
        port: 4000,
        open: true,
        tunnel: true
    });
© www.soinside.com 2019 - 2024. All rights reserved.