Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 799
Gimy boya Avatar asked Mar 15 '26 16:03

Gimy boya


1 Answers

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.

like image 52
liberborn Avatar answered Mar 17 '26 16:03

liberborn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!