Can't access nodejs port on browser virtual server - node.js

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);
});

Related

Node.js express ERR_CONNECTION_REFUSED in VMware

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.

Getting Node, Running on OS X, to Respond to HTTPS Connections

I have a small node program.
console.log("Program Started");
var http = require('http');
//Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Processing Request for URL" + request.url);
response.writeHead(200, {"Content-Type": "text/html"});
response.end("Hello World \n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
console.log("Program Ended");
If I run this program on my MacBook and access the following URL, I see my Hello World message.
http://localhost:8000/
However, I'd like to have this URL responding to HTTPS connections as well.
https://localhost:8000/
How can I do this with NodeJS?
Can the native http library do this for me? Or will I need Express?
Yes you can simply do It via https module :
1 you will have to create certificats , you can follow this auto here for macOS
https://stackoverflow.com/a/42298344/8395557

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/

why does the node.js file stop working when I add a hostname to the file?

I'm running an apache and ubuntu server on aws.
Both the below files run but when I access the server only the top one returns Helloworld. Why is that?
var http = require('http');
var port = 3000;
var server = http.createServer(function(req, res) {
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello World</h1>');
});
server.listen(port);
The bottom file runs but doesn't when I hit the server I get an error. I tried replacing hostname with the ip of the server, I also tried making the host name 127.0.0.1 that didn't work either. The error I get is
"This site can't be reached, <ip> refused to connect, ERR_CONNECTION_REFUSED"
is the ip of the server.
var http = require('http');
var hostname = 'localhost';
var port = 3000;
var server = http.createServer(function(req, res) {
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello World</h1>');
});
server.listen(port, hostname);
Cheers
If you're listening on localhost, you have to make the request from the same machine/server and not from the outside.

nodejs program not work in hosted website. works well in localhost

A simple Node.js program server.js
It works well in localhost.
But once move into a hosted website environment, it does not work.
Node.js program server.js
var http = require("http");
var port = 8888;
var serverUrl = "localhost";
var server = http.createServer(function(req, res) {
console.log("coming ");
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("HI");
res.end();
});
console.log("Listening at " + serverUrl + ":" + port);
server.listen(port, serverUrl);
It works well in localhost environment.
But once move into hosted account, it does not work.
var port = 8888;
var serverUrl = "www.mywebsite.com";
When go to www.mywebsite.com:8888, it says "The connection time out".
And there is no any message in nodejs console.
Does it work if you leave out the host parameter in server.listen? I.e. try
server.listen(port);
instead of
server.listen(port, serverUrl);

Resources