Reputation: 300
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.
Upvotes: 0
Views: 5379
Reputation: 361
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/.
Upvotes: 0
Reputation: 1306
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/
Upvotes: 3