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)
}
Related
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
I have nodejs running on pm2 and its listening on port 5000. If i run netstat -na |grep 5000 on the server i get
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN
But when my react app makes a call to http://localhost:5000/patients i get
::ERR_CONNECTION_REFUSED
Here is the code for the server:
const app = express();
const port = process.env.PORT || 5000;
app.use('/patients', require("./routes/patientRoute"));
app.listen(port, "0.0.0.0", () => {
console.log(`server is running on port: ${port}`);
})
and from React production build i run GET with axios to http://localhost:5000/patients thats when the err occurs
Also i have opened the port on firewalld
first i had to get rid of the "0.0.0.0", then i replaced http://localhost:5000 with /api/ on all of my axios calls and then in my nginx conf file i needed to add location
/api/ { proxy_pass http://127.0.0.1:5000; }
.....the location block in NGINX was the key!
I've two node.js servers: one is http, and the other is https
//HTTP server
http.createServer(function(request,response){
unifiedServer(request,response);
}).listen(config.httpPort,function(){
console.log('listening at port ' + config.httpPort)
});
//HTTPS server
var httpsServerOptions = {
'key': fs.readFileSync('./https/key.pem'),
'cert': fs.readFileSync('./https/cert.pem')
};
https.createServer(httpsServerOptions,function(request,response){
unifiedServer(request,response);
}).listen(config.httpsPort,function(){
console.log('listening at port ' + config.httpsPort)
});
//Instantiating the servers
var unifiedServer = function(request,response){....
When I run it, it will console.log listening at port 3000 (http) and listening at port 3001 (https)
3000 works just fine but.. When going into 3001 I get This page isn’t working
I've checked in case the key and certifications might be the problem, but as far as I can see they are doing their work just fine.
Any insights into this problem are appreciated
You need https on the URL to the 3001 server:
https://localhost:3001/home
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;
}
}
}
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