Mahdi.Pishguy Mahdi.Pishguy - 5 months ago 16
Node.js Question

Nodejs get throw er; // Unhandled 'error' event

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)

Answer

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

Comments