Using browser-sync with node.js app

后端 未结 4 1730
执念已碎
执念已碎 2021-01-12 11:30

I have an existing node app. My Node directory structure is setup like this:

./
  node_modules/
  src/
    views/
      index.html
      ...
    server.js
           


        
4条回答
  •  鱼传尺愫
    2021-01-12 12:05

    You already have a node server so i think what you need is Proxy. And i would also suggest you to use nodemon for going one step ahead in your speed up development thing. It will automatically restart your node development server in case of any changes. So a sample gulpfile in your case(with nodemon) might look like

    var gulp = require('gulp');
    var browserSync = require('browser-sync');
    var reload = browserSync.reload;
    var nodemon = require('gulp-nodemon');
    
    
    gulp.task('browser-sync', ['nodemon'], function() {
        browserSync.init(null, {
            proxy: "http://localhost:3700", // port of node server
        });
    });
    
    gulp.task('default', ['browser-sync'], function () {
        gulp.watch(["./src/views/*.html"], reload);
    });
    
    gulp.task('nodemon', function (cb) {
        var callbackCalled = false;
        return nodemon({script: './src/server.js'}).on('start', function () {
            if (!callbackCalled) {
                callbackCalled = true;
                cb();
            }
        });
    });
    

    ~

提交回复
热议问题