gulp + browser-sync Cannot GET / error - gulp

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 up the browser it will not display my html file and it will say "Cannot GET /" error in the browser window. This is my gulpfile.js code
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']);
i am using windows and git bash and version is "browser-sync": "^2.12.5" so what is the problem and try to explain for me in order to get something out of it.

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
}
});
});
...

I got this to work when the index.html file was inside the same folder:
browserSync.init({
server: {
baseDir: "./"
}
});

gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./"
},
port: 8080,
open: true,
notify: false
});
});

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
});

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
About <!-- Cannot GET error-->
About <!-- same code as above, but works if your site structure is: ./about/index.html-->
About <!-- 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

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"
}

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

Related

How to fix the 'Cannot GET/' error with BrowseSync?

I'm working with a Gulp setting, that will watch my HTML, Sass and files. How to solve the error below ?
Cannot GET/
This is my code:
//Watch Files
htmlWatchFiles = './src/html/main/*.html';
//Input files
// File for gulp-sass compiler
var inputScssFile = './src/stylesheets/**/*.scss'
function sassCompiler() {
return gulp.src(inputScssFile)
.pipe(sass())
.pipe(autoPrefixer())
.pipe(dest('./src/stylesheets/output'))
.pipe(browserSync.stream());
}
Here is my function to watch:
function toBrowseSync(){
browserSync.init({
server: {
baseDir: "src/",
index:"./src/html/main/index/html"
}
});
gulp.watch(inputScssFile,sassCompiler);
gulp.watch(htmlWatchFiles).on('change', browserSync.reload);
}
exports.default = series(sassCompiler,toBrowseSync);
For the BrowserSync configuration, the path used in the index property should be relative to the baseDir used. The documentation here also mentions this in a code comment.
browserSync.init({
server: {
baseDir: "src/",
index:"html/main/index.html"
}
});

Gulp 'watch' not picking up changes after moving to Gulp 4 with BrowserSync

Just migrated over to Gulp 4 from Gulp 3.9.1 and whilst everything is working fine when I run gulp and it compiles everything fine and I can start the watch command fine so it appears like it's watching for changes, whenever a change happens it doesn't seem to be able to pick it up.
Here is my watch task:
gulp.task('watch', gulp.series('browser-sync', function() {
gulp.watch(['scss/**/*.scss'], gulp.series('css-minify'));
gulp.watch(['js/dev/**/*.js'], gulp.series('js-watch'));
}));
browser-sync task:
gulp.task('browser-sync', function() {
browserSync.init({
open: 'external',
proxy: "mysite.local",
host: 'mysite.local',
// port: 5000,
browser: "chrome",
});
});
Is there another change required in Gulp 4 that I am missing here?
Ok, seems that in Gulp 4 it requires a stream, promise, event emitter, child process or observable to be returned from a function or task.
So once I changed:
gulp.task('browser-sync', function() {
browserSync.init({
open: 'external',
proxy: "mysite.local",
host: 'mysite.local',
// port: 5000,
browser: "chrome",
});
});
to:
gulp.task('browser-sync', function(done) {
browserSync.init({
open: 'external',
proxy: "mysite.local",
host: 'mysite.local',
// port: 5000,
browser: "chrome",
});
done();
});
...it was all working again.
Note the addition of the parameter passed to the anonymous function and then evoked at the end of the function - as explained in the linked article.

Browser-sync is not refreshing files observed by gulp-watch

In my build process I want to put the artifacts of the build steps into .tmp/serve directory.
I managed to set up gulp-watch to fire specific tasks when I modify files in my working directory. E.q. when I modify htmls, they are built and the artifacts are pasted into desired .tmp/serve directory
I'm having problems with reloading the files from .tmp/serve with browser-sync. When I save changes in my working directory multiple times, browser-sync refreshes only the previous changes (1 change delay). I guess the reload function fires before gulp-dest finishes. Note that if I refresh browser manually the current changes appear.
Am I doing something wrong?
Build htmls task
gulp.task('html', ['styles'], () => {
return gulp.src('app/*.html')
.pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
.pipe(gulp.dest('.tmp/serve'))
;
});
Sever task
const reload = browserSync.reload;
gulp.task('serve', ['styles', 'fonts', 'build:scripts', 'html', 'images'], () => {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp/serve'],
routes: {
'/bower_components': 'bower_components'
}
}
});
/*********** SEE THE LINES BELOW **************/
gulp.watch([
'.tmp/serve/**/*',
]).on('change', reload);
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/fonts/**/*', ['fonts']);
gulp.watch('app/*.html', ['html']);
gulp.watch('app/scripts/**/*', ['build:scripts']);
gulp.watch('bower.json', ['wiredep', 'fonts']);
});
You can use run-sequence to reload after a build.
Your gulpfile would become like this..
var gulp = require('gulp'),
runSeq = require('run-sequence')
...
;
gulp.task('build', ['styles', 'fonts', 'build:scripts', 'html', 'images']);
gulp.task('serve', ['build', 'watch'], () => {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp/serve'],
routes: {
'/bower_components': 'bower_components'
}
}
});
gulp.task('reload', function (callback) {
browserSync.reload();
callback();
});
gulp.task('watch', ['watch-styles', ...]);
gulp.task('watch-styles', function () {
gulp.watch('app/styles/**/*.scss', function () {
runSeq('styles', 'reload');
});
});
/* And so on for every watch task */
Note that run-sequence needs your tasks to either return a stream or to call a callback, otherwise it will cause gulp to pause indefinitely. Read the Doc to learn more.

Gulp browsersync pipe not working

I'm trying to get browsersync (with stream) working, but it doesn't seem to work in pipe context. The sass compiles correctly, then nothing happens. My code is as follows:
gulp.task('sass', function () {
gulp.src('../css/source/*.scss')
.on('error', function (err) {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "<%= error.message %>",
sound: "Beep"
})(err);
this.emit('end');
})
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(sourcemaps.write())
.pipe(concat('build.css'))
.pipe(gulp.dest('../css/build/dev'))
.pipe(browserSync.reload({stream:true}))
.pipe(csso())
.pipe(gulp.dest('../css/build/min'));
});
gulp.task('default', ['browser-sync'], function(){
gulp.watch('../css/source/*.scss', ['sass']);
});
In my terminal it does seem to register it though:
[BS] 1 file changed (build.css)
[BS] File changed: ../css/build/dev/build.css
The only way I can get a reload to happen is to add it to the watch task:
gulp.watch("../css/build/dev/*.css", browserSync.reload);
... but with this method I can't seem to enable the stream, as it doesn't accept a function. I have also tried browserSync.stream() as per the current docs to no avail.
Here is the browsersync task if it helps:
gulp.task('browser-sync', ['sass'], function() {
browserSync.init({
proxy: 'local.test.com',
host: '10.0.1.40'
});
});
Please help!
Remove the pipe to browserSync in task sass, and add a files key to browserSync.init():
browserSync.init({
...
files: ['../css/**/*.css'],
...
});
With the above setup you need neither gulp.watch() (that's for starting tasks based on file changes) nor a gulp task that depends on browserSync. Simply run gulp browser-sync.
Note that this watches the CSS directly to avoid a race condition between the scss changes and browserSync.

Gulp plugin for running a PHP server?

I'm looking to transition over from grunt to gulp. However I'm not finding a way to serve PHP files with livereload support, such as gateway (https://www.npmjs.org/package/gateway) using mounts. Are ther any plugins out there for running/server PHP using a gulp task?
I asked totally the same question few weeks ago. I want to start a native PHP server under Gulp, because I like the syntax better than Grunt. I also want to use PHP just to include other HTML files. :) It turns out there is a 'gulp-connect-php' plugn which has a very similar syntax to 'grunt-php' plugin.
https://www.npmjs.com/package/gulp-connect-php
https://www.npmjs.com/package/grunt-php
Here is my code to Gulp:
var gulp = require('gulp'),
livereload = require('gulp-livereload'),
connectPHP = require('gulp-connect-php');
gulp.task('connect', function() {
connectPHP.server({
hostname: '0.0.0.0',
bin: 'C:/php/php.exe',
ini: 'C:/php/php.ini',
port: 8000,
base: 'dev',
livereload: true
});
});
I also setted the exe and ini file location.
If you interested in, this is the code for Grunt:
php: {
watch: {
options: {
livereload: true,
bin: 'C:/php/php.exe',
ini: 'C:/php/php.ini',
base: '../development',
port: 8000
}
}
}
I hope it helps!
I ended up using gulp-connect-php with http-proxy. In the end, my php serve task looked like this:
gulp.task('php-serve', ['styles', 'fonts'], function () {
connect.server({
port: 9001,
base: 'app',
open: false
});
var proxy = httpProxy.createProxyServer({});
browserSync({
notify: false,
port : 9000,
server: {
baseDir : ['.tmp', 'app'],
routes : {
'/bower_components': 'bower_components'
},
middleware: function (req, res, next) {
var url = req.url;
if (!url.match(/^\/(styles|fonts|bower_components)\//)) {
proxy.web(req, res, { target: '{ip address taken out}:9001' });
} else {
next();
}
}
}
});
// watch for changes
gulp.watch([
'app/*.html',
'app/*.php',
'app/scripts/**/*.js',
'app/images/**/*',
'.tmp/fonts/**/*'
]).on('change', reload);
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/fonts/**/*', ['fonts']);
gulp.watch('bower.json', ['wiredep', 'fonts']);
});