Running a simple script on a web server using NodeJS - node.js

I'm trying to make a simple JS script run on my web server using NodeJS :
var http = require('http');
var server = http.createServer(function(req,res) {
res.writeHead(200,{"Content-Type":"text/plain"});
res.end("Hello World!");
});
server.listen(8000);
console.log('Server running');
Output when I run this on local :
curl http://127.0.0.1:8000
Hello World!
However, when I try to curl onto my web server it timeout.
I don't know if it's important or not but I have apache2 installed on the web server. Any help would be very much appreciated.

You can't access node.js from outside because it is listening on localhost IP i.e 127.0.0.1. You need to configure node.js to listen on 0.0.0.0 so it will be able to accept connections on all the IPs of your machine.
Try this:
server.listen(8000, "0.0.0.0");
If it still doesn't work and you are using iptables you may have to open port 8000. Try this:
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT

Related

AWS public ip with port not working for nodejs

I have installed nodejs in aws ubuntu 18.04 version. Added port 3000 in security Group. but node js not working in my public address with port for eg: http://3.xx.xx.xx:3000.
Note: pm2 running with node js
The solution for this is :-
in your backend inside app.js listen on port 3000 or which ever port you wish to.
Go to amazon console, got to security groups associated with your ec2 instance, and under incoming connection add a custom tcp rule with port 3000 and source should be 0.0.0.0/0 and save it
Run netstat -pan | grep 3000 if you noticed that node.js listen on 127.0.0.1 only not 0.0.0.0 so You can't access node.js from outside because it is listening on localhost IP i.e 127.0.0.1.
You need to configure node.js to listen on 0.0.0.0 so it will be able to accept connections on all the IPs of your machine.
For example:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "0.0.0.0");
console.log('Server running at http://0.0.0.0:3000/');
last step if you did not already, go to the Security Groups tab in the EC2 console. Right click the security group you setup and click edit inbound rules.
Click Add Rule. This time we are going to use a custom TCP rule on port 3000, open to anywhere.

nodejs app wont create server, says ERR_CONNECTION_TIMED_OUT

I'm trying to run a nodejs app on my VPS but for somereason I get a ERR_CONNECTION_TIMED_OUT error.
Here is my code:
const http = require('http');
const server = http.createServer((req, res) => {
res.end();
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(8000);
when i cd to my directory and do
node index.js
, it gives me no error, but going to the browser and doing: mysite.com:8000 gives me nothing. The connections times out
EDIT:
when i do curl: enomshop.tk:8000, I get some feedback. Its like i can access from the within the VPS but no access publicly
This is the way you should go about resolving such issues
Check if the process is running
Run ps aux | grep <process> to check if your process is running
Check if the port is being used
Run sudo netstat -plant | grep <port> to check if port is listening or not and is it your process
Check if the service is responding locally
Run telnet 127.0.0.1 <port> and make sure you get a response. If you don't get a response then there are few possible issues you can look into
The process is not started the server listening
The process is not using the correct port
There is a firewall that is blocking the port. There is this for firewall-cmd, and this for ufw
Check if the service is responding externally
You can do telnet <external ip> <port>, if it doesn't work then there few things you should check below
Make sure your server is binding to 0.0.0.0 or <private ip> and not 127.0.0.1
Make sure the VPS server you are hosting on, has your <port> enabled for communication. This is different for each service provider
If you follow these steps, it would solve most common network issues in such cases
I was facing the same issue. To resolve that, just open up / allow the port from network security group (VPS) or Firewall to worldwide access.
And check telnet mysite.com port, it worked for me. Kindly let me know if you still face any issue, I will try to help further.
You will need to open up the 8000 port on your VPS or redirect the traffic from any open port on VPS to 8000

This site can’t be reached on Nodejs Express?

I'm totally new to Node and I tried to run a test site on a hosting centos 7 (vultr.com). I've got nodejs, express installed.
Hello.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
run node hello.js
On my PC, http://x.x.x.x:3000/ => shows This site can’t be reached
x.x.x.x took too long to respond.
UPDATE:
I think you should consider about your server port. Have you open port 3000 in CentOS?
You can check your open port by typing
iptables -L
I think the firewall blocked your port you can open it by type this command
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
If you are using remote server, probably your 8080 port is blocked.
If you have root access and port 80 is open you can try and run script with sudo
But the first option is probably your problem
If you use the Google Cloud platform, you can open port 3000 at FIREWALL RULES in VPC network.
It works for me.

NodeJs (Express) wont listen to the IP and host in ubuntu

I have deployed my app to ununtu.
this is the bin/www:
app.set('port', 3000);
app.set('host','app.site.com');
var server = http.createServer(app);
server.listen(port,'64.143.255.122');//I put here a fake IP (deployed with the real one)
server.on('error', onError);
server.on('listening', onListening);
I have created a host in my PC: 64.143.255.122 app.site.com and I open the browser in: http://app.site.com:3000 and it does not work.
But, if I go via lynx, inside the server, and write lynx http://localhost:3000 it will work, I will get the correct page.
What might be the problem?
Thanks
Did you make iptables to allow it?
try this:
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT

Can't connect to nodejs server

I run Apache on my server. Going to my address x.x.x.x:port loads the index.html page in /var/www. When I stop the server, I can no longer connect (all good).
Now I start the node server with node server.js (the server.js file below is also located in /var/www).
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, 'x.x.x.x');
console.log('Server running at http://x.x.x.x:port/');
This gives the error listen EADDRNOTAVAIL, but I am not running any other node server (there is no other process running at this port).
I have also tried omitting the IP address and just listening thus: listen(port);
This returns no errors, but I cannot connect to the server (Browser says: Firefox can't establish a connection to the server at x.x.x.x:p.)
I have found out the problem. You don't need to specify a host name:
listen(port, 'x.x.x.x')
should just be
listen(port)
otherwise the server will not accept any connection except ones directed at the specified ip.
The port is in use or not available. Try a different port like:
listen(88, 'x.x.x.x');
and see if that connects. Also, make sure that x.x.x.x is actually the ip address of your server. You can listen on all IPs by doing:
listen(88, '0.0.0.0');
or by leaving the host/ip section out entirely. If it does connect on another port, you just need to find what is using the port you want. If it's port 80, use:
sudo netstat -tulpn | grep :80
to get the program using that port.
Sounds like the port is locked up and in use..
The following command will give you a list of node processes running.
ps | grep node
To free up that port, stop the process using the following.
kill <processId>

Resources