首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在文件更改时重新加载浏览器之前,使用Gulp watch、Axios和Browsersync执行HTTP get

在文件更改时重新加载浏览器之前,使用Gulp watch、Axios和Browsersync执行HTTP get
EN

Stack Overflow用户
提问于 2020-04-25 03:47:50
回答 1查看 331关注 0票数 0

我正试着看两套文件夹。其中一组只需要在更改时重新加载浏览器。第二种方法需要在重新加载之前,通过一个单独的后台http get来“重新初始化框架”。

我当前的尝试正确地处理了一次,但只有一次。你能告诉我为什么和如何修复它吗?麻烦的部分在第二个监视任务中。

代码语言:javascript
运行
复制
const gulp = require('gulp');
const axios = require("axios");
const bs = require('browser-sync').create(); 
const { reload } = bs;

const url = "http://127.0.0.1:80/healthcheck?fwreinit=1";

var paths = {
        refresh: [
            "./layouts/**/*.*",
            "./views/**/*.*",
            "./includes/**/*.js",
            "./includes/**/*.css"
        ],
        reinit: [
            "./handlers/**/*.*",
            "./models/**/*.*",
            "./interceptors/**/*.*",
            "./config/**/*.*"
        ]
    }

gulp.task('watch', () => {
    gulp.watch(paths.refresh, (done) => {
        reload();
        done();
    });
    gulp.watch(paths.reinit, () => {
        console.log("Reinitializing framework");
        axios.get(url)
        .then(response => {
            console.log(response.data.trim());
            reload();
        })
        .catch(error => {
            console.log("Error:  Please ensure you have a /healthcheck route set up in /config/router.cfc!");
            console.log("Error:  Once you've done that, please shut down commandbox then try browsersync again.");
        });
    });
});

gulp.task('proxy', () => {
    bs.init({
        proxy: "localhost:80",
        port: 81,
        open: true,
        notify: false
    });
});

gulp.task('default', gulp.parallel('watch', 'proxy'));
EN

回答 1

Stack Overflow用户

发布于 2020-04-25 04:12:28

Gulp传递一个“完成”回调,必须调用该回调才能继续。将代码更改为以下代码解决了问题。

代码语言:javascript
运行
复制
const gulp = require('gulp');
const axios = require("axios");
const bs = require('browser-sync').create(); 
const { reload } = bs;

const url = "http://127.0.0.1:80/healthcheck?fwreinit=1";

var paths = {
        refresh: [
            "./layouts/**/*.*",
            "./views/**/*.*",
            "./includes/**/*.js",
            "./includes/**/*.css"
        ],
        reinit: [
            "./handlers/**/*.*",
            "./models/**/*.*",
            "./interceptors/**/*.*",
            "./config/**/*.*"
        ]
    }

gulp.task('watch', () => {
    gulp.watch(paths.refresh, (done) => {
        reload();
        done();
    });
    gulp.watch(paths.reinit, (done) => {
        console.log("Reinitializing framework");
        axios.get(url)
        .then(response => {
            console.log(response.data.trim());
            reload();
            done();
        })
        .catch(error => {
            console.log("Error:  Please ensure you have a /healthcheck route set up in /config/router.cfc!");
            console.log("Error:  Once you've done that, please shut down commandbox then try browsersync again.");
        });
    });
});

gulp.task('proxy', () => {
    bs.init({
        proxy: "localhost:80",
        port: 81,
        open: true,
        notify: false
    });
});

gulp.task('default', gulp.parallel('watch', 'proxy'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61416339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档