1

this is my firs program on NodeJs and i', trying to use Express and Socket.io on that. after create simple project as below code, i get

throw er; // Unhandled 'error' event

error, i'm google more tutorials but i cant find whats my code problem

install packages:

{
    "name": "signalAndroidServerApplication",
    "version": "0.0.0",
    "private": true,
    "dependencies": {
        "body-parser": "~1.12.4",
        "cookie-parser": "~1.3.5",
        "express": "~4.12.4",
        "socket.io": "latest"
    }
}

my nodejs:

var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3000;

server.listen(port, function () {
    console.log('Server listening at port %d', port);
});

io.on('connection', function (socket) {
    socket.on('new_count_message', function (data) {
        console.log('new_count_message' + data);
        io.sockets.emit('new_count_message', {
            new_count_message: data.new_count_message
        });
    });
});

full error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1039:14)
    at listen (net.js:1061:10)
    at Server.listen (net.js:1135:5)
    at Object.<anonymous> (/var/www/signal/nodeJs/server.js:8:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
3
  • 3
    The real error is EADDRINUSE – the address is already in use. Commented Jun 13, 2016 at 11:45
  • 1
    EADDRINUSE means the port is already used. Commented Jun 13, 2016 at 12:24
  • To avoid this error, be sure to use ctrl-c instead of ctrl-z to shut down your server. superuser.com/questions/262942/… Commented May 24, 2018 at 16:42

1 Answer 1

2

Error: listen EADDRINUSE means that the port you're trying to run on is already being used.

Try changing to it to use another port.

So var port = process.env.PORT || 3000; change this to var port = process.env.PORT || 4000; and hit localhost:4000

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.