gulp + browser-sync Cannot GET / error

前端 未结 8 2503
悲哀的现实
悲哀的现实 2021-02-15 12:40

I am learning the front-end build system currently gulp, i want to use brower-sync and the problem is it is not throwing an error in the commad line but instead when it brings

相关标签:
8条回答
  • 2021-02-15 13:00

    I solved my "Cannot GET" errors in browser-sync by always pointing to a .html file in code, or by having a folder structure ending with and 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 -->
    

    My Gulp file for reference (using browser-sync with jekyll, so everything is in the ./_site folder, and the gulpfile is in ./)

    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']);
    

    Hope it helps.

    Michelangelo

    0 讨论(0)
  • 2021-02-15 13:03

    I got this to work when the index.html file was inside the same folder:

        browserSync.init({
            server: {
                baseDir: "./"
            }
        });
    
    0 讨论(0)
  • 2021-02-15 13:04
    gulp.task('server', function()
    {
            browserSync.init(["public/src/css/","public/src/js"],{ 
                server: "./",
                startPath: "./index.html", // After it browser running
                //    browser: 'chrome',
                host: 'localhost',
                //       port: 4000,
                open: true,
                tunnel: true    }); 
    });
    
    gulp.task('default', ['scripts', 'styles', 'html', 'server', 'watch']);
    
    0 讨论(0)
  • 2021-02-15 13:10

    May be you have not index.html in baseDir: '_site/'? In order to see your static web-page in the web-browser instead of that annoying message you have to rename a file your_file.html to index.html. This will solve Cannot GET/ problem.

    0 讨论(0)
  • 2021-02-15 13:15

    In my case, this helped me:

    server: {
                baseDir: '.',
                index: "index.html"
            }
    
    0 讨论(0)
  • 2021-02-15 13:17

    Is there an index.html file in your ./public/ folder? If not, you need to tell browserSync what is your start page. You can also get browserSync to show the listing of the base directory, see update below.

    You could also try to use public without the leading dot.

    Edit: The startPath config directive does not seem to work, use index instead

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

    Update: Actually you can get directory listing with browserSync. That way it would show a list of files in public, not the Cannot Get error

    ...
    gulp.task('browser-sync', function() {
       browserSync({
         server: {
            baseDir: "public/",
            directory: true
         }
       });
    });
    ...
    
    0 讨论(0)
提交回复
热议问题