Using browser-sync with node.js app

ぃ、小莉子 提交于 2019-12-19 07:23:34

问题


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

./
  node_modules/
  src/
    views/
      index.html
      ...
    server.js
  test/
  gulpfile.js
  package.json

I can successfully start my app my running node ./src/server.js from the root shown above. Once started, I can visit "http://localhost:3000" in the browser and see the contents of index.html like I am expecting.

I want to speed up my development and I recently learned about browsersync. In an attempt to include it in my gulp process, I have the following:

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

browserSync.init({
  server: {
    baseDir: './src/',
    server: './src/server.js'
  }
});

When I run gulp, I see the following in the command-line:

BS] Access URLs:

 --------------------------------------
       Local: http://localhost:3000
    External: http://[ip address]:3000
 --------------------------------------
          UI: http://localhost:3001
 UI External: http://[ip address]:3001
 --------------------------------------

My browser is then opened and it attempts to load http://localhost:3000. At this point, I see the following error in the browser window:

Cannot GET /

What am I doing wrong? I can successfully visit http://localhost:3000 if I start my app using node ./src/server.js, however, its like its not running with BrowserSync. What am I doing wrong?


回答1:


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();
        }
    });
});

~




回答2:


Why do you want to use the built-in server if you have your own in ./src/server.js ?

Check this, What server in browsersync does is create a static server for basic HTML/JS/CSS websites, so you might need to use the proxy feature as shown here. This means that you need to run your server as normally and wrap it up in the proxy.




回答3:


Using the express generator default folder structure with the start script in bin\www, and using the ejs template, this is how i modified my gulpfile.js :

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:8000", // port of node server
    });
});

gulp.task('default', ['browser-sync'], function () {
    gulp.watch(["./views/*.ejs"], reload);
});

gulp.task('nodemon', function (cb) {
    var callbackCalled = false;
    return nodemon({
        script: './bin/www', 
        env: {
        PORT: 8000
    }}).on('start', function () {
        if (!callbackCalled) {
            callbackCalled = true;
            cb();
        }
    });
});

Notice that am watching for any files that end in .ejs. I also got a problem when using nodemon with the port in use, so i added an env to pass the port as 8000, env: { PORT: 8000 }




回答4:


Since the tag grunt is missing from the question, here's a solution that works using only NPM (package.json):

"scripts": {
    "start": "browser-sync start --serveStatic 'src' --serveStatic 'node_modules' --files 'src'"
 }

Now all the <script> src attributes can be relative:

<script src="/stats-js/build/stats.min.js"></script>


来源:https://stackoverflow.com/questions/35576663/using-browser-sync-with-node-js-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!