简体   繁体   English

吞并重装

[英]Gulp with live-reload

I have a website that I've built with Node. 我有一个用Node构建的网站。 I can successfully start and run the site by running node server.js from the command-line. 我可以通过从命令行运行node server.js来成功启动和运行站点。 I then access the site by visiting " http://localhost:3000 " in my browser. 然后我在浏览器中访问“ http:// localhost:3000 ”访问该站点。 I'm now trying to improve some of the build process surrounding the site. 我现在正在尝试改进网站周围的一些构建过程。 To do that, I'm relying on Gulp. 要做到这一点,我依靠Gulp。

I want to automatically reload the webpage when a change is made to the HTML or CSS files. 我想在对HTML或CSS文件进行更改时自动重新加载网页。 I stumbled upon the gulp-livereload plugin. 我偶然发现了gulp-livereload插件。 I have installed it as described in the docs. 我按照文档中的描述安装了它。 However, when I visit " http://localhost:35729/ " in the browser, I just see the following: 但是,当我在浏览器中访问“ http:// localhost:35729 / ”时,我只看到以下内容:

{
  minilr: "Welcome",
  version: "0.1.8"
}

My gulp task is configured like this: 我的gulp任务配置如下:

gulp.task('launch',  function() {
    var toWatch = [
        'src/**/*.html',
        'src/**/*.css'
    ];

    livereload.listen();
    gulp.watch(toWatch, function() {
        console.log('reloading...');
        livereload();
    })
}

I do not see my home page like I do when I visit " http://localhost:3000 " after running node server.js . 在运行node server.js后,当我访问“ http:// localhost:3000 ”时,我看不到我的主页。 What am I missing? 我错过了什么?

Live reload is a protocol to send notifications to the browser to do a reload. 实时重新加载是一种向浏览器发送通知以进行重新加载的协议。 But you need a browser plugin to listen and do the reload, although it is possible to forgo the plugin using a script tag. 但是你需要一个浏览器插件来监听和重新加载,尽管可以使用脚本标签放弃插件。

The gulp-livereload plugin only concerns itself with the implementation of the livereload server, you still need to serve the files via a http server from gulp. gulp-livereload插件只关注livereload服务器的实现,你仍然需要通过gulp的http服务器提供文件。

You can use a gulp module that does both gulp-connect . 您可以使用同时执行gulp-connect的gulp模块。

However unless you are tied to livereload for some reason, I suggest using browserync. 但是,除非您因某种原因与livereload绑定,否则我建议使用browserync。 Browsersync is a nice alternative to livereload that aditionally adds the capacity of syncing your view between browsers. Browsersync是livereload的一个很好的替代方案,它可以增加在浏览器之间同步视图的能力。 Since it injects a script tag into your html and listens on a socket you don't need any plugins to make it work. 因为它会在你的html中注入一个脚本标签并监听一个套接字,所以你不需要任何插件来使它工作。 It works even on Mobile devices. 它甚至可以在移动设备上运行。

A gulp task to use browsersync might look like this. 使用browsersync的gulp任务可能如下所示。 Don't forget to add browsersync to your package.json file 不要忘记将browsersync添加到package.json文件中

var browserSync = require('browser-sync').create();

gulp.task('serve',  [] , function( cb ){

    browserSync.init({
        open:  true ,
        server: {
            baseDir: "src"
        }
    });

    gulp.watch([
      'src/**/*' ,
    ] , ['serve:reload'] );

});

gulp.task('serve:reload' , []  , function(){
  browserSync.reload();
});

Why are you visiting ' http://localhost:35729/ ' ? 你为什么要访问' http:// localhost:35729 / '? If this is port where livereload is listening then it won't show your site, because as you said your site is available from ' http://localhost:3000 '. 如果这是livereload正在侦听的端口,那么它将不会显示您的网站,因为正如您所说,您的网站可从“ http:// localhost:3000 ”获得。

I assume that you have correctly configure gulp. 我假设您已正确配置gulp。 By it I mean that livereload is listening, and you watch changes in your files and in pipes you have '.pipe(livereload()'. 通过它我的意思是livereload正在监听,你观察你的文件和管道中的变化'.pipe(livereload()'。

  1. You have to install https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei 您必须安装https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei
  2. you have to run your app ' http://localhost:3000 ' in chrome browser. 你必须在Chrome浏览器中运行你的应用程序' http:// localhost:3000 '。
  3. you will have in browser plugin bar new icon (this is icon of this plugin) 你将在浏览器插件栏中有新图标(这是此插件的图标)
  4. you have to click this icon to run this plugin 你必须单击此图标才能运行此插件
  5. finish

Now when you change something in files, gulp watcher will notice this, do some work, and inform chrome plugin that there are changes, This plugin will refresh your project page. 现在,当您更改文件中的内容时,gulp观察者会注意到这一点,做一些工作,并告知chrome插件有更改,此插件将刷新您的项目页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM