【问题标题】:grunt-contrib-connect works only with keepalivegrunt-contrib-connect 仅适用于 keepalive
【发布时间】:2014-12-21 20:47:42
【问题描述】:

当我尝试连接到服务器时,发生了一些奇怪的事情。 终端没有错误,它说“在http://0.0.0.0:5555" 上开始连接网络服务器,然后没有错误地完成。但是当我向http://localhost:5555/ 服务时 - 我得到典型的“找不到页面”。 在 chrome 控制台中,一警告一错误:

i18n-values: Missing value for "primaryParagraph"
http://localhost:5555/:1 GET http://localhost:5555/ net::ERR_CONNECTION_REFUSED

更令人惊讶的是,当我定义 keepalive:true 时,一切正常。真的,我不明白为什么会这样。

我的 Gruntfile.js

'use strict';

module.exports = function( grunt ) {

// tasks
grunt.initConfig({
    // compile SASS
     sass: {
        dist: {
          files: [{
            expand: true,
            flatten: true,
            src: ['assets/source/sass/*.scss','app/shared/**/*.scss','app/components/**/*.scss'],
            dest: 'assets/source/css/',
            ext: '.css'
          }]
        }
    },

    // concat and minify CSS
    cssmin: {
        styles: {    
            files: {
                'public/css/style.min.css': ['assets/source/css/*.css','assets/source/libs/**/*.css','app/shared/**/*.css','app/components/**/*.css']
            }
        }
    },

    // autoprefix
    autoprefixer: {
        options: {
          browsers: ['> 1%', 'Android 2', 'last 2 versions', 'Firefox ESR', 'Opera 12.1', 'ie 7', 'ie 8', 'ie 9']
        },
        no_dest: {
          src: 'public/css/style.min.css'
        }
    },

    // compile Coffeescript
    coffee: {
      compile: {
        options: {
            separator: ';'
        },
        files: {
          'public/js/scripts.js': ['app/*.coffee','app/shared/**/*module*.coffee','app/shared/**/*.coffee','app/components/**/*module*.coffee','app/components/**/*.coffee']
        }
      }
    },

    // concat and minify JS
    concat: {
        options: {
            separator: ';'
        },
        scripts: {
            src: ['assets/source/libs/angular/angular.js', 'assets/source/js/*.js','assets/source/libs/*.js','assets/source/libs/**/*.js'],
            dest: 'public/js/vendor.js'
        }
    },

    uglify: {
        scripts: {
            files: {
                'public/js/scripts.min.js': 'public/js/scripts.js',
                'public/js/vendor.min.js': 'public/js/vendor.js'
            }
        }
    },

    // watch
    watch: {
        options: {
            livereload: true
        },
        scripts: {
            files: ['app/*module*.coffee','app/*routing*.coffee','app/*.coffee','app/shared/**/*module*.coffee','app/shared/**/*.coffee','app/components/**/*module*.coffee','app/components/**/*.coffee','assets/source/js/*.js','assets/source/libs/*.js','assets/source/libs/**/*.js'],
            tasks: [ 'coffee', 'concat', 'uglify' ]
        },
        css: {
            files: ['assets/source/sass/*.scss','app/shared/**/*.scss','app/components/**/*.scss','assets/source/css/*.css','assets/source/libs/**/*.css','app/shared/**/*.css','app/components/**/*.css'],
            tasks: [ 'sass', 'cssmin', 'autoprefixer' ]
        }
    },

    // minification
    imagemin: {
        dynamic: {
            files: [{
                expand: true,
                cwd: 'assets/img/',
                src: ['*.{png,jpg,gif}'],
                dest: 'public/img/'
            }]
        }
    },

    // bower
    bower: {
        install: {
          options: {
            targetDir: 'assets/source/libs/',
            layout: 'byComponent',
            cleanBowerDir: false
          }
        }
      },

    // server
      connect: {
        server: {
          options: {
            port: 5555
          }
        }
      }

});

grunt.loadNpmTasks( 'grunt-contrib-coffee');
grunt.loadNpmTasks( 'grunt-contrib-concat');
grunt.loadNpmTasks( 'grunt-contrib-uglify' );
grunt.loadNpmTasks( 'grunt-contrib-watch' );
grunt.loadNpmTasks( 'grunt-contrib-sass' );
grunt.loadNpmTasks( 'grunt-contrib-cssmin' );
grunt.loadNpmTasks( 'grunt-autoprefixer' );
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-bower-task');
grunt.loadNpmTasks('grunt-contrib-connect');

// some default tasks

grunt.registerTask('default', [ 'coffee', 'concat', 'uglify' , 'sass', 'cssmin', 'autoprefixer']);
grunt.registerTask('publish', [ 'bower', 'coffee', 'concat', 'uglify' , 'sass', 'cssmin', 'autoprefixer']);
grunt.registerTask('serv', [ 'connect', 'watch']);
};

【问题讨论】:

    标签: gruntjs localhost


    【解决方案1】:

    很抱歉,这只是how Grunt connect works

    请注意,此服务器仅在 grunt 运行时运行。一旦 grunt 的任务完成,Web 服务器就会停止。此行为可以通过 keepalive 选项进行更改,并且可以通过运行诸如 grunt connect::keepalive 之类的任务来临时启用。

    这是一个有意的功能。 Grunt connect 仅用于在运行构建或测试任务时使用,它不是用于玩网站的本地服务器。

    如果您需要用于静态文件 (html/css/js/image) 的小型本地服务器,那么我会推荐 npm module "http-server"。它非常易于安装和运行:

    ~$ npm install -g http-server
    ~$ cd /path/to/your/project
    ~$ http-server
    

    祝你好运。

    【讨论】:

    • 非常感谢您的解释和 nmp 服务器链接。我通过运行 grunt 和 watch task 解决了这个问题
    猜你喜欢
    • 2015-01-25
    • 2014-05-24
    • 2017-06-12
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多