Node.js express ERR_CONNECTION_REFUSED in VMware - node.js

I have set up vmware workstation, Ubuntu 16.06 desktop as my guest machines, windows 10 as host.
nodejs 9.4.0
express 4.16.2
and simple server HTTP and express:
const http = require('http'),
PORT = 4000;
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('It works!');
res.end();
}).listen(PORT);
console.log("Listening on port " + PORT);
**/
/*** EXPRESS BASED TEST*/
const express = require('express');
const app = express();
PORT = 4000;
app.get('/', (req, res)=>{
res.send("it works!");
})
app.listen(PORT, 'localhost', ()=>{
console.log('Listening on port: ' + PORT)
});
/****/
What happens is, when I try to open test webpage from my host browser when guest runnin express - I get ERR_CONNECTION_REFUSED. BUT when i start simple http server - it connects! I tried to connect with telnet, ssh... turn of firewall... behavior is the same...
Is it something wrong with express? Is there a way to fix it?

The solutions is here:
Node.js connect only works on localhost
In my express app I make it listen on localhost, which is 127.0.0.1 and which is loopback address. After changing it to 0.0.0.0 the problem was solved.

Related

Can't access nodejs port on browser virtual server

I'm running a nodejs application on my virtual host.
I just want to see the results of my index.js port 3000 on browser (I have tested all open ports) , but I will get connection time out with domain or server Ip.
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var message = 'It works!\n',
version = 'NodeJS ' + process.versions.node + '\n',
response = [message, version].join('\n');
res.end(response);
});
server.listen(3000,function(){
console.log("Listening on port : " + 3000);
});
And I will start with node index.js
I can see the results with ssh command
curl https://localhost:3000
But when I can't see the results in browser
Any idea for this?
You trying to access server via https protocol, however your server supports http
You need to use the default port by node js and the code you need to write is as follows
server.listen(3000 || process.ENV.PORT,function()
{
console.log("Listening on port : " +process.ENV.PORT);
});

Cannot connect to Local Node.js server running in WSL2

I have been trying to run a simple node.js server using express from WSL2 at port 3000
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('<h1>Hello World</h1>');
});
app.listen(3000, () => {
console.log('Run at http://localhost:3000');
});
I can connect from in my computer's browser and see the 'Hello World' but I'm not able to connect from a external device's browser (My phone,etc...) i put my windows ip both wsl2 ip , but none works
also i tried to set a host in '0.0.0.0' like this but it didn't work either
app.listen(3000, '0.0.0.0', () => {
console.log('Run at http://localhost:3000');
});
When i try to run the same server but using git bash in my windows everything works alright...
Any suggestions ?
I'd appreciate your help!
I had the same problem and found the answer in a reddit discussion
Change o route from "/" to "/test" and return a "ok" and you'll se that your server is responding

How to open a NAS Synology port for Node.js?

I am new and want to start using Node.js.
I try to run an “Hello, world!” on Synology:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
This code runs:
~$ node app.js
Server running at http://127.0.0.1:3001/
But nothing was found when accessing Synology:
ERR_CONNECTION_REFUSED
I found the instruction:
Open Firewall-Port on Synology depending on which port you are going to run your xxx (e.g. 3001)
OR
Create volume1/path_to_your_app/app.js and let the http server listen to port 8080.
Where is this setting on Synology?
Your 127.0.0.1 needs to be the IP address of your NAS. If you are on your router locally and you have your NAS set to for example 192.168.3.10, then your code would be:
const hostname = '192.168.3.10';
const port = 3001;
Then in your web browser you would type 192.168.3.10:3001 after running app.js
-> go to Control panel, then Firewall, Firewall-profile. There you can edit your firewall-rules and alternatively add or remove rules.
I hope this is what you are searching for. Otherwise write what you are looking for

Can't access nodejs server on aws

I have an AWS EC2 instance which I am using for my node js application. I cannot access any application page on the server.
I wrote this simple code solely for testing purposes but I cannot access even this from my browser.
var express = require('express');
var app = express();
app.listen(3000, ()=> {
console.log('listening');
});
app.get('/',(req,res)=> {
res.send('hi');
});
On navigating to http://:3000, I should be able to see "hi" written but the request times out.
Here are my security group configs :
Solved the problem with some help.
Since the port available to me was port 80, so I just forwarded the port 8080 to port 80 via. port forwarding and it worked out.
Sharing the link from where I found the solution:installing nodejs and forwarding port on aws
My code (shown below) is a bit different, but I was having the same problem with connecting from a remote browser:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
What I had to do was replace const hostname with the complete AWS server IP: ec2-xx-xx-xx-xx.compute-1.amazonaws.com:3000/
Make sure you have port 3000 open in your security group, or this will not work.
I was then able to connect to the NodeJS server from my browser on my PC.
Hope this helps. The corrected code is shown below (replace the x's with your actual IP address. You can get this on your EC2 dashboard.
const http = require('http');
const hostname = 'http://ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com/';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I got the code for this NodeJS server from:
https://websiteforstudents.com/install-the-latest-node-js-and-nmp-packages-on-ubuntu-16-04-18-04-lts/

Linux nodejs rest service work only localhost

i'm developing a REST API with node js on linux.
This my first sample code to try if all works fine:
'use strict';
var express = require("express");
var app = express();
var port = process.env.PORT || 8080 ;
app.listen(port, "127.0.0.1", function(){
console.log("Express server is listening on port ", port);
});
i have try to call localhost:8080 into linux server and works fine.
If i try to call trhe IP of server with this port by a external IP the express server not responding.
Any help about this?
Thanks
Your second parameter to the app.listen() api, which you set to "127.0.0.1" is the hostname. This will cause node to only listen for requests matching that host name from the request domain.
This is an optional parameter.
Perhaps try the app.listen(port, callback) form of the api?
app.listen(port, function () {
console.log("Express server is listening on port ", port);
});
app.get('/', function (req, res) {
res.send('Hello world!')
});
Or you can move the [hostname] parameter to a configuration file if you don't want your service to listen to every host name.
See the server.listen() documentation for better insight into parameter default behaviour.
Try this so your app listens on all network interfaces and not only on localhost:
app.listen(port, "0.0.0.0", function(){
console.log("Express server is listening on port ", port);
});

Resources