简体   繁体   中英

Express combined with browser-sync with node

I am very new to the world of JavaScript and i am trying to build my development environment for the first time what i want to achieve should be simple...

I want to run an Express server along with browser-sync for hot reloading i am not using gulp or grunt

but i keep getting this error:

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

this is what my my scripts look like in the package.jason file

"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"
},

in the 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)
}
});

and in the bs-config.js file is the default one i just changed ui to false

This question gave me the hint to use the proxy but i still get that error that i have no idea why ...please enlighten me i want to understand what's wrong

That error means you already have something running on port 3000. Likely another instance of the same project running in a different window. :)

I faced the similar issue. Looks like BrowserSync is not working with Express on the same port anymore. I managed to run both of them on different ports with connect-browser-sync middleware.

It is my 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') + '...');
});

On port 3000 there is an Express server (with no page auto reload). And on port 3001 there is BrowserSync server serving the same content with page auto reload.

I don't know why but without npm-run-all browser-sync automatically shifted the port to 3001. After I tried using npm-run-all it seemed to not automatically shift the port. So I think you have to specify it like this:

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

Or at least that worked for me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM