Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Server is timing out in browser but cURL request works

I am developing on a Ubuntu server that I do not own. This is my first node application.

Node.js has been installed on the server. I have created a simple server file:

Server.js

var http = require("http");
http.createServer(function (request, response){
    response.writeHead(200, {'Content-Type': 'text/plain'});

    //response to send out
    response.end('Hello sof');

    //print to screen
    console.log('request processed\n');
}).listen(1234);
console.log("Server Running on 1234");

In putty I run the server with this command:

node Server.js

In another instance of putty I run this command:

curl http://my.url.website.ca:1234/*curl request*/

In the putty instance where I ran the curl command I get this output:

Hello sof

In the putty instance where I ran the node Server this get printed to the screen:

request processed

So it looks like the server is running. However when I put http://my.url.website.ca:1234 into my browser I get this error:

GET http://my.url.website.ca:1234/ net::ERR_CONNECTION_TIMED_OUT

and nothing is printed to the screen in putty window where the server is running. Am I doing something wrong or missing something in my code? Or is this a configuration issue?

like image 688
Ethan Melamed Avatar asked Mar 02 '16 20:03

Ethan Melamed


1 Answers

I linked the server person to this page and he confirmed that the issue was in fact the firewall. The port I was trying to use was not open. The port was then opened and I was able to connect through the browser.

I then tested it with my jquery application and had a CORS error. For anyone else running into the same issue as me you will probably have that issue next. I fixed it by adding these headers:

response.setHeader('Access-Control-Allow-Origin', 'http://my.website.ca');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');    
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

Cheers!

like image 154
Ethan Melamed Avatar answered Oct 13 '22 23:10

Ethan Melamed