How to set up a nodejs application server in ubuntu - node.js

I don't understand why i have this problem which i show the output in the second part. While following the tutorial "how To Set Up a Node.js Application for Production on Ubuntu 14.04" i did everything.
I created a script to test my private ip address like this
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'myPrivateIp');
console.log('Server running at http://myPrivateIp:8080/');
The problem that i encountered while testing the apllication.
JoeDoe#myUbuntu:~$ node hello.js
Server running at http://myPrivateIp:8080/
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL myPrivateIp:8080
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at Server._listen2 (net.js:1218:19)
at listen (net.js:1267:10)
at net.js:1376:9
at doNTCallback3 (node.js:440:9)
at process._tickCallback (node.js:346:17)
at Function.Module.runMain (module.js:477:11)
at startup (node.js:117:18)
at node.js:951:3

The second argument of listen is not the domain name, is the address to listen on. It could be something like 192.168.x.x or 127.0.0.1 or 0.0.0.0 ( means all address. ). The default is 127.0.0.1.
Which means you should remove the second argument of listen.

Related

NodeJS - Error: listen EADDRNOTAVAIL: address not available

I am running a Node app that should be hosted on a local server.
At the moment, I am sending just a plaintext response.
const http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {ContentType: 'text/plain'});
res.end("test");
});
When I listen to the localhost everything works fine and I am able to send the request from my browser.
server.listen(3000, '127.0.0.1'); // works fine, on the same machine
However, if I try to listen to a port on my LAN network by typing the router's IP, I get an error.
server.listen(3000, '192.168.0.1'); // causes an error
Error: listen EADDRNOTAVAIL: address not available 192.168.0.1:3
000
at Server.setupListenHandle [as _listen2] (net.js:1253:19)
at listenInCluster (net.js:1318:12)
at doListen (net.js:1451:7)
at process._tickCallback (internal/process/next_tick.js:63:1
9)
at Function.Module.runMain (internal/modules/cjs/loader.js:7
57:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1297:8)
at process._tickCallback (internal/process/next_tick.js:63:1
9)
[... lines matching original stack trace ...]
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I have tried this with my public IP address unsuccessfully.
Is there any way to listen to a port on a LAN server so that I can send requests from any computer on the network?
Also, I would later like my application to run on any computer on any LAN network. How can I dynamically add my host?
Try this
server.listen(3000, '0.0.0.0');
you can access your node application from other machine
e.g
your lan ip is 192.168.0.101 then you can browse 192.168.0.101:3000 from other machine
I got this error, the issue was I had a VPN connected
Be careful with your IP.
Sometimes it changes. When I work with Node I have to check the ipconfig in Shell... I hope with that it could help if someone else have the same problem...
The right IP address actually turned out to be 192.168.0.104.
The IP address of a server machine should be used, not that of a router.

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

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

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.

EADDRINUSE with Node.js on OpenShift

Writing a test application in Node.js running on OpenShift and it currently will not start.
This is my code:
#!/bin/env node
var http = require('http');
var ip = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(ip, port);
console.log('Server running');
I get the error
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1003:19)
at listen (net.js:1044:10)
at Server.listen (net.js:1104:5)
at Object.<anonymous> (/var/lib/openshift/52854c6f4382ec071400051d/app-root/runtime/repo/server.js:11:4)
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)
DEBUG: Program node server.js exited with code 8
Which seems to mean I can't bind to the port. I've found various information on why this might be but none of it seems relevant - it all seems related to permissions but surely the point of process.env.OPENSHIFT_NODEJS_* is I have permission to bind to it?
It means some other process is using your port process.env.OPENSHIFT_NODEJS_PORT. EACCESS would mean permission denied, not EADDRINUSE. Try changing to unused ports like 8080. You can check used ports with :
netstat -tulpn
netstat -tulpn | grep :$OPENSHIFT_NODEJS_PORT //filter by your port

tutorial of node.js code sample fails with Error: listen EADDRINUSE

I am a beginner programmer that is trying to learn node.js using the following tutorial site
http://www.nodebeginner.org/#hello-world
I got to the point where I was trying to set up the server but got an error with the below code
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1022:14)
at listen (net.js:1044:10)
at Server.listen (net.js:1110:5)
at Object.<anonymous> (/Users/.........../server.js:7:4)
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)
any help would be appreciated
EADDRINUSE means that address is in use.
Basically, you tried to start two servers at the same time that both use port 8888. You have to stop or kill one before starting another. The other server on port 8888 could be another process running your node script, or it could be something else in the system that serves content on port 8888.
Alternatively, you can get this if you don't let the socket settle for a few seconds after terminating the old server.
A more practical answer based on this great one.
Find out what is using port 8888 with this command:
lsof -i tcp:8888
You should get something like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 86456 myName 13u IPv6 0xa6b50fb47c9c3c81 0t0 TCP *:ddi-tcp-1 (LISTEN)
Now that you know which process is in the way, KILL IT! Softly, like so:
kill -15 86456

Resources