利用nodejs开启页面热更新

1.安装本地服务器

npm install http-server -g

2.安装gulp

npm install gulp -g
npm install gulp --save-dev

3.安装browser-sync(使用管理员方式打开命令行)

npm install --save-dev browser-sync

在文件根目录开启http-server

http-server

PS E:\APP\xampp\htdocs\WebRoot> http-server
Starting up http-server, serving ./
Available on:
http://192.168.191.2:8080
http://127.0.0.1:8080
Hit CTRL-C to stop the server

记住此时的端口网址等会备用

4.添加gulpfile.js文件

var gulp = require("gulp");
var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;
gulp.task('serve', function () {
var files = [
        './**/*.css',
        './**/*.js',
        './**/*.html'
];

browserSync.init({
    files: files,
    //书写上一步的端口地址
    proxy: "http://localhost:8080/"
});
gulp.on("change", reload);
})
//默认任务,一旦执行了gulp,在没有执行具体执行某任务的情况下,执行此任务
gulp.task("default", function() {
    gulp.start(["serve"]);
    })

5.运行gulp

PS E:\APP\xampp\htdocs\WebRoot> gulp
[21:39:00] Using gulpfile E:\APP\xampp\htdocs\WebRoot\gulpfile.js
[21:39:00] Starting 'default'...
[21:39:00] Starting 'serve'...
[21:39:00] Finished 'serve' after 25 ms
[21:39:00] Finished 'default' after 27 ms
[Browsersync] Proxying: http://localhost:8080
[Browsersync] Access URLs:
--------------------------------------
   Local: http://localhost:3001
External: http://192.168.0.109:3001
 --------------------------------------
      UI: http://localhost:3002
 UI External: http://192.168.0.109:3002
 --------------------------------------
[Browsersync] Watching files...
[Browsersync] Reloading Browsers...
[Browsersync] Reloading Browsers...
[Browsersync] Reloading Browsers...

browser-sync内集成了weinre

很方便的调试手机上的页面。

你可能感兴趣的:(node)