【问题标题】:Express combined with browser-sync with nodeExpress 结合 browser-sync with node
【发布时间】:2017-05-31 15:28:56
【问题描述】:

我对 JavaScript 世界很陌生,我正在尝试构建我的开发环境,我想实现的目标应该很简单......

我想运行 Express 服务器以及浏览器同步以进行热重载,我没有使用 gulp 或 grunt

但我不断收到此错误:

Starting the application in the development mode...
[BS] Proxying: http://localhost:3000
[BS] Access URLs:
 ------------------------------------
    Local: http://localhost:3000
 External: http://10.111.234.196:3000
 ------------------------------------
[BS] Watching files...
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::3000

这就是我的脚本在package.jason 文件中的样子

"scripts": {
"prestart": "babel-node buildScripts/startMessage.js",
"start": "npm-run-all --parallel security-check open:src",
"open:src": "babel-node buildScripts/srcServer.js & browser-sync start --proxy 'localhost:3000' --files 'src' --no-ui",
"security-check": "nsp check"
},

srcServer.js

import express from 'express';
import path  from 'path';
import open  from 'open';

const port = 3000;
const app = express();


app.get('/', function(request, response){
response.sendFile(path.join(__dirname, '../src/index.html'));
});

app.listen(port, function(err){
if(err){
console.log(err);
}else{
open('http://localhost:'+ port)
}
});

bs-config.js 文件中是默认的,我刚刚将ui 更改为false

This question 给了我使用代理的提示,但我仍然收到我不知道为什么的错误...请赐教我想了解什么是错误的

【问题讨论】:

    标签: javascript node.js express automation browser-sync


    【解决方案1】:

    我不知道为什么,但是没有npm-run-all browser-sync 会自动将端口转移到 3001。在我尝试使用 npm-run-all 后,它似乎没有自动转移端口。所以我认为你必须像这样指定它:

    "browser-sync": "browser-sync start--proxy=localhost:3000 --port=3001 --files=views"

    或者至少对我有用:)

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。看起来 BrowserSync 不再与 Express 在同一个端口上工作。我设法使用 connect-browser-sync 中间件在不同的端口上运行它们。

      这是我的 server.js

      var express = require('express'),
          bodyParser = require('body-parser'),
          http = require('http'),
          path = require('path'),
          cors = require('cors');
      
      
      // Express server
      var app = module.exports = express();
      app.set('port', process.env.PORT || 3000);
      
      
      // BrowserSync at development host
      if (app.get('env') === 'development') {
          var browserSync = require('browser-sync'),
          var bsconf = {
              host: 'localhost',
              port: 3001,
              https: false,
              notify: false,
              open: false,
              online: false,
              ui: false,
              server: {
                  baseDir: path.join(__dirname, '/public')
              },
              files: [path.join(__dirname, '/public/**/*.{html,css,js,json}')]
          };  
          var bs = browserSync.create().init(bsconf);
      
          app.use(require('connect-browser-sync')(bs));
      }
      
      app.use(bodyParser.json());
      app.use(bodyParser.urlencoded({ extended: true }));
      app.use(express.static(path.join(__dirname, 'public')));
      app.use(cors());
      
      
      // Route index.html
      app.get('/', function(req, res) {
          res.sendFile(path.join(__dirname, 'public', 'index.html'));
      });
      
      
      // Starting express server
      http.createServer(app).listen(app.get('port'), function () {
          console.log('Listening on port ' + app.get('port') + '...');
      });
      

      在端口 3000 上有一个 Express 服务器(没有页面自动重新加载)。并且在端口 3001 上有 BrowserSync 服务器通过页面自动重新加载提供相同的内容。

      【讨论】:

        【解决方案3】:

        该错误意味着您已经在端口 3000 上运行了一些东西。可能同一项目的另一个实例在不同的窗口中运行。 :)

        【讨论】:

        • 我已经杀死了所有节点实例并再次尝试了所有我得到的是浏览器上的无限加载和控制台上的错误......顺便说一句,我的逻辑很好,有快递和浏览器-一起同步 浏览器同步吸引我的地方是即使在共享时也能保持热重载功能,是否有替代方法?
        • 终于成功了!但它在不同的端口上,所以现在我打开了两个选项卡,一个在 3000 上打开,另一个在 3001 上打开,如何让两者都在同一个端口上?
        猜你喜欢
        • 2017-01-20
        • 2017-05-15
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 2017-03-22
        • 2020-01-17
        • 2016-04-22
        • 1970-01-01
        相关资源
        最近更新 更多