I can't run my node application in my on prem VM - node.js

I'm trying to run a node js application on an on-prem VM which is running RHEL 7. I'm not experienced in RHEL 7 and can't seem to find any details in running Node JS apps on it.
My app is super simple. Is a server which returns a message...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome Node.js');
}).listen(8000);
console.log('Server running on http://ip:8000');
I run the application and try accessing the IP with the port in the browser within the correct network. Am I missing something? My assumption was RHEL7 was the server and would show me the application once I open it on the browser.

Related

starting a node.js page on localhost - the site can't be reached

I'm on Chrome / Windows 7 and trying to make my first node.js steps using this tutorial:
https://www.w3schools.com/nodejs/nodejs_get_started.asp
So myfirst.js is:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Going on http://localhost:8080/ I'm getting the error:
This site can’t be reached localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
Firewall is completely off.
Any help?
You need to initiate myfirst.js with node, on the CLI by typing the command for it to run
node myfirst.js
check your firewall
you must edit in firewall system Node.js as public
enter image description here
enter image description here
server.listen(port, hostname, () => {
console.log(Server running at: http://${hostname}:${port}/);
});
port=8080
hostname=127.0.0.1 or 192.168.1.x(your ip)
maybe 8080 is use by other software
I had the same problem but then I installed the pug inside the folder I was working on (basically the folder I named for my website) again and it worked. (idk why did I have to install the package inside the folder as it was installed already outside that folder)

Using Node on Network Server

How does node work? I have installed node on the server. On the same network is my work computer where I have git bash and my project files. Am i unable to run my files on my work computer as long as I call the right port number? I am running the code below on my local machine calling the ip address and port number. But then I am getting this error. Error 0x2 starting node.exe index.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!n');
}).listen(1337, 'ip');
console.log('Server running at http://ip:1337/');
Normally this is due to a corrupted binary, I would reinstall both git and node to fix problem.

Can't access node.js script from web browser

I installed node.js and socket.io in my CentOS 6.4 server. It is running parallel with Apache, with Apache as main server (port 80)
I wrote a simple Hello world script to test node.js installation:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World\n');
response.end();
}).listen(8088);
console.log('Server started');
If I run it in command line I get 'Server started'. But when I tryh to access it via web browser, typing http://xxx.xxx.xxx.xxx:8088 it never loads. I've tried to use many other port numbers with no success. I have to ips in my server, but neither of them work, nor my domain addres under those ips.
How could I fix it?
EDIT: node,js is installed in another server, and I'm trying to access it via webbrowser from outside this server.
Thanks!
i think you need to open port 8088 by firewall on server.
see man iptables

Node.js on MAC: Access a Node.js web server from another computer

I built a Node.js web server on my computer, using the so-well-known-http-web-server-example of Node.js:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('It works');
}).listen(3000, '127.0.0.1');
This works (as expected) on the computer that runs the server.
I would like to access the server from another computer, in the same LAN. Using ifconfig on the terminal of the computer that runs the server (Apple MacOSX), I get: 192.168.0.6.
So, in my other computer, I opened my browser and connected to http://192.168.0.6:3000, but I get:
Oops! Google Chrome could not connect to 192.168.0.6:3000
My final aim, is to be able to connect to the server using my smartphone.
Any help would be welcome. Don't hesitate to ask for more details if necessary.
Thanks in advance :)
127.0.0.1 is only local interface. Try to start listening all interfaces:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('It works');
}).listen(3000, '0.0.0.0');

Using Cloud9 IDE on an EC2 instance

I've installed Cloud9 IDE on an Amazon EC2 instance and started it with this line:
node bin/cloud9.js
But when I open the IDE address from the browser, there is no response. I've added the port 3000 in the instance security group. I think the problem is I'm trying to load the page from a url such as 'http://ec2-XXX-XXX.compute-1.amazonaws.com:3000/' where the Cloud9 server expects a request url like 'http://127.0.0.1:3000'. I get the page content if I 'wget' 'http://127.0.0.1:3000' from the EC2 instance, so the server is working.
Similar thing happens with node.js 'hello world' example, I get no response if the server 'listens' like this,
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
because of the ip parameter, and it works if I change the line to .listen(1337);
What should I change in Cloud9 IDE to make it work through 'http://ec2-XXX-XXX.compute-1.amazonaws.com:3000/'?
You can try running this command and connect to it through localhost:3000. Had the same problem with couchdb.
ssh ec2-XXX-XXX.compute-1.amazonaws.com -L 3000:localhost:3000
The issue is the IP address cloud 9 is bound to by default is the local host address, you should be able to change the value in cloud 9's config to 0.0.0.0 (listen on all addresses)

Resources