0

I'm trying to test a simple Node.js server on my webserver. The problem is that I can't access the Node.js server from my chrome browser and I have searched without any success.

This is my basic server.js script (server side)

var app = require('express')();

app.get('/test', function (req, res) {
    console.log('web page opened');
});

app.listen(3000, function () {
    console.log('Listening on port 3000');
});

When I run the node in console I get result as expected

public_html$ node server.js
Listening on port 3000   

Now when I try to access the URL like this:

http://xxx.xxx.xxx.xxx:3000/test the connection times out and I do not get anything.

This prevents me from using $.ajax form to send data to my node server and the jquery request fails with error as connection timed out issue as well because the URL:3000 with my nodejs port is not accessible.

It seems like my host (Cloudways) does not allow access on any port. If that is case, what can I do really in this situation?

Note: I do not have root access to the server, they can't give root access for security.

6
  • Sounds like you need a new host Commented Feb 23, 2018 at 0:36
  • There is not any work around for this issue? Sorry new to nodejs here. Commented Feb 23, 2018 at 0:37
  • 2
    If the host only allows external connections on a specific port, for example, 80 or 443, you'll either want to have node.js run on that port, or, setup a webserver infront of node.js that reverse proxies requests from 80/443 to 3000 Commented Feb 23, 2018 at 0:38
  • Thanks Kevin. Looks like I indeed need a new host indeed.. I've been trying for hours without any success .. they also do not allow stopping apache on port 80 so I could use node on port 80. pretty useless :/ Commented Feb 23, 2018 at 0:39
  • Do you have access to create virtual hosts in apache? that's all you would need to reverse proxy to your node server. Commented Feb 23, 2018 at 0:41

2 Answers 2

3

Actually going thru Apache to access your app server, nodeJs in your case, is the standard way to avoid security vulnerabilities etc.. You can configure your Apache server as follows:

<VirtualHost *:80> 
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName www.example.com
  ServerAlias example.com
  ProxyPass / http://localhost:3000/test/
  ProxyPassReverse / http://localhost:3000/test/
</VirtualHost> 

The you would call your app at http://xxx.xxx.xxx.xxx:80/test or simply at http://xxx.xxx.xxx.xxx/test since 80 is implied for HTTP and apache will call http://xxx.xxx.xxx.xxx:3000/test for you.

EDITED: This should work thru .htacccess too - which is what, it seems cloudways wants you to do : https://support.cloudways.com/what-can-i-do-with-an-htaccess-file/

Sign up to request clarification or add additional context in comments.

4 Comments

Sweet! I have one concern tho. Would not that impact nodejs performance as I have like a proxy or bridge to server the request and still need to use Apache? I mean would that solution damage the whole point of using nodejs?
In some ways - Apache will increase performance by allowing you to serve static resources much more efficiently than nodeJs can.
Thanks! I just discovered that cloudways does not give access to apache config as well. So would this code still work inside .htaccess?
support.cloudways.com/how-to-manage-server-settings . -> Try the setting the "Default application" there to your nodeJs URL.
0

Best way to test node.js is to use node process manager on your server like https://www.npmjs.com/package/pm2. This will save a lot of time for deployment. but you will need root access to install this package. if you does not have root access then ask your hosting provider to DO install these packages for you. If in any case you did not get root access and you have no access to open port and install packages for your project. Then use any other hosting like https://www.linode.com/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.