Node.js web server - node.js

I would like to access http://mypublicIP:2888 with Node.js running on mypublicIP and port. The server times out! Does anyone know why this is happening? What can I try to do to identify the underlying problem?
EDIT
I believe if you don't specify the IP address, Node should be reachable from all network interfaces. Node code:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(2888);
// Put a friendly message on the terminal
console.log("Server running somewhere....");

on the apache machine:
check that node is actually listening on port 8000
check that it's reachable from localhost
check that the request appears in apache's access_log
check if anything relevant appears in apache's error_log
these translate to (on Linux):
netstat -tnlpe | grep -w 8000
telnet localhost 8000
tail /var/log/apache/access_log
tail /var/log/apache/error_log

Related

server side node.js request couldnt connect to host

I try using server side node.js and write a simple -general- javascript code for http request
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.sendHeader(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.close();
}).listen(1337);
sys.puts("Server running 8080");
when i execute on server it's work and give me out put "Server running 8080" string.
But couldnt connect to host when i check on browser.
(i try this http://myserverip:1337)
I open 1337 port but still same
Same thing on Linux. Check your network connectivity. If you are on Amazon, check your security groups. It has be something that is blocking that port.

Running a node.js server on my VPS on port 3000 and the connection times out

In hostgator I have a VPS running centOS. I installed NodeJS and screen.
I added the following code to a file named index.js:
//1
var http = require('http');
//2
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 3000.');
On 'screen:1' I run the following command:
node index.js
It gives me the console output stating 'Server running on port 3000.'
I switch to 'screen:0' and run the following command:
curl localhost:3000
and I get the following response:
<html><body><h1>Hello World</h1></body></html>
Yet, when I try my server's IP address (substitute the xxx for a real IP address, cause I'm not disclosing my VPS IP address):
xxx.xxx.xxx.xxx:3000
The page never comes up and eventually it times out.
I've tried various ports (8080, 7000) and to not avail.
Do I need to place the iOS project in a different directory.
Currently I have it in /root/Projects/NodeTutorial2/index.js.
What do I need to do to get a hello world response from my VPS?
If you're getting a response from on the box, but not from other boxes, it's almost certainly a firewall issue. Turning off IPTables or allowing the traffic in on the port in question is one option but an easier / more appropriate option is to simply have your app use port 80 (for HTTP) or 443 (for HTTPS). You can either do that by listening to that port on the app directly, or by having a web server that acts as a reverse-proxy for you (e.g. NGINX or Apache).

cannot connect to server from other computer

I have a server running CentOs 6.3 with the latest version of node installed on it.
when I execute this code
var http = require('http');
var server = http.createServer(function (request, response) {
console.log('c');
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(80,"0.0.0.0",function(){
console.log(this._connectionKey);
});
// Put a friendly message on the terminal
console.log("Server running");
I get the following messages:
Server running
4:0.0.0.0:80
so far so good, that mean the server should be running.
But, when I try to access the site via the ip the host company gave me I get a "not found" message.
I tryend also ommiting the Ip, changing it to 127.0.0.1 and even to the external Ip. I also changed the port to 1337 and gurnisht, it didnt help.
P.S. This code sample is a test to check what is wrong, I have a more complex server using express with the same problem.
Tnx
well I found the problem.
I had my firewall still working.
I simply stopped the iptables
service iptables stop

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>

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

Resources