Search code examples
htmlimagegulplocalhost

Localhost doesn't display img but index.html does


i have a problem with at localhost. I use gulp server for my project and i want to display img but when i use localhost it doesn't display but when i open my index.html file it works fine

<img src="src/img/logo.png" alt="Logo">

I can't find a good src to display it. How it should looks like?

edit:

my gulpfile is:

const gulp = require("gulp");
const sass = require("gulp-sass");
const sourcemaps = require('gulp-sourcemaps');
var connect = require('gulp-connect');

gulp.task('connect', function(cb) {
  connect.server({
    root: './dist',
    livereload: true
  });
  cb();
});

gulp.task("sass", function() {
  return gulp.src('./src/scss/main.scss')
          .pipe(sourcemaps.init())
          .pipe(sass({errLogToConsole: true}))
          .pipe(sourcemaps.write())
          .pipe(gulp.dest('./dist/css'))
          .pipe(connect.reload());
});

gulp.task('html', function () {
  return gulp.src('./src/*.html')
    .pipe(gulp.dest('./dist'))
    .pipe(connect.reload());;
});

gulp.task('watch', function () {
  gulp.watch('./src/scss/**/*.scss', gulp.series('sass'));  
  gulp.watch('./src/**/*.html', gulp.series('html'));  
});

gulp.task('default', gulp.series('connect', 'watch'));

Solution

  • Based on your gulp configuration your HTML files from src are moved (Part 1) into your ./dist/ folder and then served from there (Part 2). So move your index.html into src and remove it from the img tag.

    Moving HTML to dist folder (Part 1):

    gulp.task('html', function () {
      return gulp.src('./src/*.html')
        .pipe(gulp.dest('./dist'))
        .pipe(connect.reload());;
    });
    

    Gulp server on dist folder (Part 2):

    gulp.task('connect', function(cb) {
      connect.server({
        root: './dist',
        livereload: true
      });
      cb();
    });
    

    So what you need is either a task that moves your images as well or the easy way: Create a img folder in dist containing the image and change the path in your index.html as follows:

    <img src="img/logo.png" alt="Logo">
    

    I would recommend to change your gulp config, so it serves from your src folder for development and create a second task for a build process (Including minify css, js and moving the files). I just add a config that i used some time ago:

    var gulp = require('gulp');
    var sass = require('gulp-sass');
    var browsersync = require("browser-sync").create();
    var babel = require('gulp-babel');
    var sourcemaps = require('gulp-sourcemaps');
    var concat = require('gulp-concat');
    var uglify = require('gulp-uglify');
    var plumber = require('gulp-plumber');
    var cssnano = require('gulp-cssnano');
    var imagemin = require('gulp-imagemin');
    var cache = require('gulp-cache');
    var del = require('del');
    var rev = require('gulp-rev');
    var revdel = require('rev-del');
    var collect = require('gulp-rev-collector');
    
    // Development Tasks 
    // -----------------
    
    // BrowserSync
    function browserSync(done) {
      browsersync.init({
        server: {
          baseDir: "./src/"
        },
        port: 3000
      });
      done();
    }
    
    // BrowserSync Reload
    function browserSyncReload(done) {
      browsersync.reload();
      done();
    }
    
    // Watchers
    function watchFiles(){
      gulp.watch('src/scss/**/*.scss', gulp.series('sassify', browserSyncReload));
      gulp.watch('src/*.html', gulp.series(browserSyncReload));
      gulp.watch('src/js/**/*.js', gulp.series(browserSyncReload));
    }
    
    // Optimization Tasks 
    // ------------------
    
    // Sassify
    gulp.task('sassify', (cb) => {
      gulp.src('src/scss/**/*.scss') // Gets all files ending with .scss in src/scss and children dirs
        .pipe(plumber())
        .pipe(sass()) // Passes it through a gulp-sass, log errors to console
        .pipe(gulp.dest('src/css')) // Outputs it in the css folder
        .pipe(browsersync.stream());
      cb();
    });
    
    // Optimizing CSS
    gulp.task('css', (done) => {
      gulp.src('src/css/*.css')
        .pipe(plumber())
        .pipe(cssnano())
        .pipe(gulp.dest('dist/css'));
      done();
    });
    
    // Optimizing JS
    gulp.task('js', (cb) => {
      gulp.src('src/js/*.js')
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(babel({
          presets: ['@babel/env']
        }))
        .pipe(uglify())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('dist/js'));
        cb();
    });
    
    // HTML
    gulp.task('html', (cb) => {
      gulp.src('src/*.html')
        .pipe(gulp.dest('dist'));
      cb();
    });
    
    // Optimizing Images 
    gulp.task('images', function(done) {
      gulp.src('src/assets/**/*.+(png|jpg|jpeg|gif|svg)')
      // Caching images that ran through imagemin
      .pipe(cache(imagemin({
        interlaced: true,
      })))
      .pipe(gulp.dest('dist/assets'))
      done();
    });
    
    // Generate Revisions...
    gulp.task('revision:rename', function(){
      gulp.src(["dist/**/*.css",
                "dist/**/*.js"])
      .pipe(rev())
      .pipe(revdel())
      .pipe(gulp.dest('dist'))
      .pipe(rev.manifest({ path: 'manifest.json' }))
      .pipe(gulp.dest('dist'))
    });
    
    // update references
    gulp.task('revision:updateReferences', function(){
       gulp.src(['dist/manifest.json','dist/**/*.{html,json,css,js}'])
       .pipe(collect())
       .pipe(gulp.dest('dist'))
    });
    
    // Cleaning 
    gulp.task('clean', function() {
      return del.sync('dist').then(function(cb) {
        return cache.clearAll(cb);
      });
    })
    
    gulp.task('clean:dist', (cb) => {
      del.sync(['dist/**/*', '!dist/assets', '!dist/assets/**/*']);
      cb();
    });
    
    // Build Sequences
    // ---------------
    
    gulp.task('default',
      gulp.series(
        'sassify',
        gulp.parallel(
          watchFiles,
          browserSync
        ) 
      )
    );
    
    gulp.task('build',
      gulp.series(
        'clean:dist',
        'sassify',
        gulp.parallel(
          'css',
          'js',
          'images'
        ),
        'html'
      )
    );