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

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)

Related

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

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.

How to access meteor app from outside without passing through NginX?

I am hosting a meteor app on an Ubunu Linux machine. The app is listening on port 3000. If I use a webserver, like NginX and forwards the HTTP requests from port 80 to 3000 I can browse to the server from the outside and see reach the app. However, when I try to access the app directly at port 3000, i.e. browse http://myhost:3000 it just tries to connect and nothing happens.
I have made sure that all firewalls are down and that the app is listening on all interfaces, i.e. 0.0.0.0:3000, so that is not the issue.
To verify that port was actually reachable, I created a simple node js webserver:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(3000);
Now browsing to the the sever, I can see "Hello World!". So obviously this works so why I can not reach meteor has nothing to do with firewalls or unopened ports.
Thus it seems that there is something strange when trying to access a meteor app directly at port 3000. But why? I use the following environment variables:
export MONGO_URL=mongodb://localhost:27017/meteor
export HOST=myhost
export PORT=3000
export ROOT_URL=http://myhost
So what am I missing? Ports are open and I can see that the node process instance is listening on port 3000 when I run netstat -tulpan
I was using the force-ssl meteor package which makes a redirect back to the ROOT_URL without port number. So solution is to remove the package to make it work with a custom port.
I was discussing the solution on the meteor forum where I got the solution:
https://forums.meteor.com/t/can-not-access-meteor-app-without-passing-through-nginx-server/40739/11

Deploying a repository on an OpenShift node.js server

I'm using the Wercker Continuous Integration & Delivery Platform to test and deploy a BitBucket repository on a node.js OpenShift server. Wercker loads in the BitBucket repository, builds it, tests it in the node.js environment, and passes it without any issue. It's also checking the code with jsHint, and returns without any errors.
Wercker also indicates that the deployment passes without errors, as does OpenShift. The problem is that when I check the application URL provided to me by OpenShift, it results with a server error:
503 Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
In troubleshooting this, I restarted the server (I'm running the basic account, and I have that option) but that doesn't seem to resolve the issue. Neither Wercker or Openshift indicate that there is a problem, but for some reason, I'm simply unable to access that domain without error.
How can I fix this (with the most basic tier)?
This was the solution:
I installed the RHC client tools available on the OpenShift website, checked the application logs, and found that OpenShift was unable to find a server.js file in the root directory. So I renamed my app.js file to server.js, and in my package.json I changed the "start" value to server.js. Then I configured the code in server.js file to the OpenShift environment variables, and that did it!
The server.js now reads:
var http = require('http');
var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1',
port = process.env.OPENSHIFT_NODEJS_PORT || '8080';
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, ip);
console.log('Server running at http://'+ip+':'+port+'/');
I'm now able to connect to the application URL and get the basic "Hello World" response.
(If at this point you're still unable to connect to your application, restart your server, and that should do the trick.)
I hope this helps someone else in the future.
Here's a helpful resource that I leaned on: https://gist.github.com/ryanj/5267357
Your app should be able to listen to the IP and port defined by Openshift's reverse proxy.
You need to change the port number and perhaps the IP in the server configuration.
Explained here: OpenShift node.js Error: listen EACCES

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

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