How to setup node project to the linux server? - node.js

node project run completely fine on localhost but not able to run on domain, Please help me to resolve this this.
app.listen(3000,'APP_PRIVATE_IP_ADDRESS' function(err,rslt){
if(err){
console.log(err);
}
else{
console.log("App Started on PORT 3000");
}
})
when i run the node server.js on terminal it print the message "App Started on PORT 3000" but when i run on the web it shows the error site can't be reached.

Try using '0.0.0.0' symbolic IP, it means bind all IP's or any IP.
app.listen(3000, '0.0.0.0', function(err, rslt){
if(err){
console.log(err);
}
else{
console.log("App Started on PORT 3000");
}
});
Hostname is optional, so this is equivalent:
app.listen(3000, function(err, rslt){
if(err){
console.log(err);
}
else{
console.log("App Started on PORT 3000");
}
});
In the case of that you want to specify the IP version, this two usage make difference. If you provide '0.0.0.0' value for hostname parameter, only IPv4 binding happens and only IPv4 request are accepted and listened. If this parameter is not specified, both IPv6 and IPv4 bindings happen.

Related

Node server is not running and Now errors are shown in the terminal

I have a node server which when I run is not showing server is running at port 5000 for example. Also, I got 0 errors in the terminal.
My question is how to check for errors that are causing the server not to run ?
app
.listen(port, () => {
console.log(`Server is running at port: ${port}`);
})
.on('error', function (err) {
console.log(err);
});
Here is the code that I use to run to listen to requests.

Why it's create localhost:5000 CLOSE_WAIT Node?

my server started with PORT 5000, and sometimes it show next error
Error: listen EADDRINUSE: address already in use :::5000
When I Check port 5000, I see what port :5000 use for Chrome. They pass away after time( 1 min ) but, it's create more problems, i don't check code with terminal, because after error not showing console.log?
I think that chrome is launched by some development tool. Because System apps can not take port number more that 1024. So you need to check if your IDE is launching it and then check how to change the port or you can follow this link to auto increment the port if given port is not available.
var portrange = 45032
function getPort (cb) {
var port = portrange
portrange += 1
var server = net.createServer()
server.listen(port, function (err) {
server.once('close', function () {
cb(port)
})
server.close()
})
server.on('error', function (err) {
getPort(cb)
})
}
Code from link

Can't connect to Node server on Amazon EC2

Here's the app.js, the code is too long so that's why I'm showing this code only, there's no problem in other code I assume this is a network problem.
app.js
app.listen(8080, 'localhost', function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
I don't get any response when i run lsof -i :8080. but I do get response when I run curl localhost:8080 on the server.
and I don't think there's any problem with security group. I allowed any ip to access to the instance as you can see below.
and here's actually how it looks like when I test public ip and localhost
ubuntu#:ip~/$ curl -v 18.217.107.76:8080
* Rebuilt URL to: 18.217.107.76:8080/
* Trying 18.217.107.76...
* connect to 18.217.107.76 port 8080 failed: Connection refused
* Failed to connect to 18.217.107.76 port 8080: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 18.217.107.76 port 8080: Connection refused
ubuntu#ip:~/$ curl -v localhost:8080
I get response here!
I changed the code from
app.listen(8080, 'localhost', function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
to
app.listen(8080, function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
now it's working
This is what worked for me!!
In your security group you have added the rule HTTP which listens by default on port 80.
So basically if you have configured your node server to run on a port other than port number 80 (I was doing this mistake) and try to access the public DNS(EC2 public DNS can be found in instance description) on browser, connection refused error might come so what you can do is change the PORT value in the config to 80.
Your config.env will look like this
PORT=80
And in your server.js you can write
const PORT = process.env.PORT;
try {
app.listen(PORT, () => { console.log(`server running at port ${PORT}`) })
} catch (error) {
console.log(error)
}

nodejs v5.10.1 cannot get my host address anymore?

Why the latest version of nodejs (v5.10.1) cannot get my host address anymore?
express code:
var express = require('express');
var app = express();
// respond with "Hello World!" on the homepage
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log(server.address());
console.log('Example app listening at http://%s:%s', host, port);
});
result:
{ address: '::', family: 'IPv6', port: 3000 }
Example app listening at http://:::3000
It should be:
http://127.0.0.1
Any ideas how I can fix this?
I am on Linux.
Actually, maybe you will see something like
:80
and then
::80
actually this one have a pattern like this:
host:port IPv4
host::port IPv6
127.0.0.1:80 means that using IPv4, listening in 127.0.0.1 in port 80
:80 means that using IPv4 ,listening on all address in port 80
127.0.0.1::80 means that using IPv6, listening in 127.0.0.1 in port 80
and so on.
so, the ::3000 means listening to IPv6 in port 3000
These configuration was done under these variable
var host = server.address().address;
var port = server.address().port;
you can just change the value of these variable to "127.0.0.1" and "80" and see what happens, but most likely your machine still using IPv6 there, if you want to change to IPv4 for your machine, change the setting of your machine under:
Windows: control panel -> network and sharing center -> (your network) -> properties -> setup the IPv4
Linux: i dont have linux machine to test this, but the syntax should be using ifconfig or ipconfig depends on your linux, please refer for something like https://unix.stackexchange.com/questions/34093/static-ipv4-ipv6-configuration-on-centos-6-2
Mac: i dont have mac machine also, should be the same with linux so try to do the same also
I have the same problem, i solved it acessing like an array:
server.address()["port"]
Here a example:
export const onError = (server: Server) => {
console.log(server.address()["port"])
return (error: NodeJS.ErrnoException): void => {
let port: number | string = server.address()["port"];
if (error.syscall !== 'listen') throw error;
let bind = (typeof port === 'string') ? `pipe ${port}` : `port ${port}`;
switch(error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
}

telnet command in nodejs not running(on windows)

I am trying to learn nodejs. Trying to execute an example
var server = require('net').createServer();
var port = 4001;
server.on('listening', function() {
console.log('Server is listening on port', port);
});
server.on('connection', function(socket) {
console.log('Server has a new connection');
socket.end();
server.close();
});
server.on('close', function() {
console.log('Server is now closed');
});
server.on('error', function(err) {
console.log('Error occurred:', err.message);
});
server.listen(port);
when i run this .js file - node ...js I get a message - server is listening on port 4001
now i cant type anything in console (windows) the tutorial says execute telnet localhost 4001. if i do ctrl+c and then type telnet localhost 4001 it gives err 'telnet is not recognized as an internal or external command.
Open another console and run telnet localhost 4001 there.
And to install telnet, follow these instructions: http://technet.microsoft.com/en-us/library/cc771275(v=ws.10).aspx

Resources