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
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
I got this to work when the index.html file was inside the same folder:
browserSync.init({
server: {
baseDir: "./"
}
});
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']);
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.
In my case, this helped me:
server: {
baseDir: '.',
index: "index.html"
}
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
}
});
});
...