Why i can not use express node js on my server? - node.js

node js version node -v ===> v6.3.1 was install on my server.
then i will install express by this step
npm init
in entry point: (index.js)
npm install express --save
then create app.js
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
and then node app.js
and it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at EventEmitter.listen (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:6:5)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
how can i do ?

Choose a different port number as 3000 since some service is already bound to that port given that error.
Change it in the following line:
app.listen(3000, () => console.log('Example app listening on port 3000!'))

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
So in this case, there must be running a service listening on port 3000 already.

you can run this command before start your app
sudo pkill -kill node // to kill any node app running then start your app

Related

Running simple node js server

I am running a simple node js server on my Mac. I also have mamp installed. Just FYI.I am able to go to localhost:8888 just fine and preview the web page. However, in my server.js file, if I include the host as a parameter to the server.listen function like so
server.listen(127.0.0.1, 8888, function(){
console.log('Server running');
})
I get the following error in my terminal.
Error: listen EADDRINUSE 127.0.0.1
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1338:19)
at listenInCluster (net.js:1396:12)
at Server.listen (net.js:1491:5)
at Object.<anonymous> (/Users/username/Desktop/nodeServer/server.js:12:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
Users-Mac-Pro:nodeServer user$
If I just have
server.listen(8888, function(){
console.log('Server running');
})
everything works fine. Does this have anything to do with the fact that I have mamp installed? Just want to understand why one works and the other doesn't.
Try putting your host after port as following
server.listen(8888, '127.0.0.1', function() {
console.log('Server running');
})
app.listen documentation
app.listen([port[, host[, backlog]]][, callback])
Some other process might have been using 8888 port. You can either kill that process or use someother port for your nodejs server.
To get the process id of the process using port 8888, use
lsof -i tcp:8888
and kill the process using
kill -9 <pid>
Hope this helps.
Probably you're currently running your server in that port. Try with:
killall node

I'm trying to deploy a node server to heroku and I'm getting this error: throw er; // Unhandled 'error' event

I'm trying to deploy a node app to heroku and it looks like judging from other questions that heroku is dynamically assigning a port and it is somehow incompatible with something.
(I had to remove some details to post the question)
events.js:182
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1041:11)
at _exceptionWithHostPort (util.js:1064:20)
at Server.setupListenHandle [as _listen2] (net.js:1305:19)
at listenInCluster (net.js:1370:12)
at Server.listen (net.js:1466:7)
at Function.listen (/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/app/index.js:9:21)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TFP#1.0.0 start: `node index.js`
npm ERR! Failed at the TFP#1.0.0 start script.
Problem
I had the same issue. I ran into this problem when I tried to set the port manually:
app.listen(80, function () {
console.log('Example blog app listening on port 80!')
});
This led me to the same EACCES error.
Solution
I was able to solve the problem by going back and looking through the Heroku docs where I found:
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
So, assuming you're using express, if you switch the port assignment to look something like this:
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Then you application should start properly.
Solution without Express.js
If you are not using Express, then you can set the port simply as:
var PORT = process.env.PORT || 5000;
Where process.env.PORT will be your production port, and 5000 will be the port you would use when testing your server locally.
I have faced this kind of problem. The exact error is the port 80 is already enabled in another project or app. So before start your app, stop the other app mapped to 80 port.
Now you can run your app without this error.
Thanks

node.js server.listen on a specific IP address EADDRNOTAVAIL

When I try to listen on a specific IP address, I am getting an error.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, "10.211.56.1");
console.log('Server running at http://10.211.56.1:8080/');
I get the following error:
➜ node-test sudo node server.js
Server running at http://10.211.56.1:8080/
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1139:19)
at listen (net.js:1182:10)
at net.js:1280:9
at dns.js:85:18
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3
This basically means the ip/port combo you are using is not available to your server. This could be because the port is already in use, or, that ip address isn't one your server is using.
You should instead be either using localhost:someport or 0.0.0.0:someport, where someport is a port that isn't currently being used.

New to using NodeJS Express Server

I trying to view a static html page at localhost, but get the following error message:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Function.app.listen (/home/phil/xProgramming/AngJS/Project/node_modules/express/lib/application.js:536:24)
at Object.<anonymous> (/home/phil/xProgramming/AngJS/Project/server.js:6:5)
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)
I've tried killall, but I still get the same error message.
I new to Linux and am using Ubuntu 14.04
The EACCES error normally means that you are trying to bind to a port and failing. This is probably because you are trying to bind to port 80 without root permissions. Try running node with root permissions and it should work.
sudo node server.js
or
sudo nodejs server.js
Depending on how you have Ubuntu setup.
If this is a development environment instead of running as root you should use a port greater than 1024. Typically you will find people using port 3000 for node though 8000 and 8080 are also used. Here is how I normally do this when using Express.
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});

HTTPs port error on NodeJs and Express, why? banging my head on wall

Why do I get an error on my Amazon server when running my NodeJS app?
sudo node app.js
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (/home/ubuntu/www/app.js:65:38)
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)
Here is my code:
var app = express();
var server = http.createServer(app).listen(4000, function() {
console.log('Express HTTP server listening on port ' + app.get('port'));
});
https.createServer(credentials, app).listen(443, function() {
console.log('Express HTTPS server listening on port 443');
});
I think the problem is the 443 because when I delete the https.createServer "listen()" connection then everything works fine.
Thanks for your attention :)
You probably have another process running that's using this port (probably another instance of your app.
Use ps aux to see your running processes.
Use lsof -i | grep 4000 to see what's using this port.

Resources