-1

I run an Apache server on a shared host that uses cPanel. I installed node.js successfully on the server, but the problem is my host can only open outbound ports and I need to be able to access the port via browser sooo, I was wondering if I didn't have to use a port? Like for example here's my code:

const express = require('express');
const req = require('request')

var app = express();
var port = process.env.PORT || 8016;

var server = app.listen(port, () => {
    console.log('Server listening on ' + port);
});

Could I for example make the application not need a port and it goes off of the domain instead of a port like example.com? Like I want to add a app.route() would I be able to do that just in public_html?

  • A web service is not accessable without a port. The domain name you enter into a browser just hides the complexity. http://example.com will go to port 80. https://example.com will go to port 443. You should be able to configure Apache httpd to [proxy requests to your Node app](https://serverfault.com/questions/497856/using-an-apache-virtualhost-to-access-a-node-js-instance-on-the-same-server) – Matt Nov 22 '17 at 02:43

1 Answers1

1

All server processes must listen on a port.

When you see a URL like http://www.example.com, there is an implied port. If the protocol is http and there is no port in the URL, then the browser will automatically use port 80. So, if that URL reaches a server, it's because the server at example.com is listening on port 80. The default port for https is 443.

Could I for example make the application not need a port and it goes off of the domain instead of a port like example.com

No. Your application server needs to listen on a port. As I said above, you can leave the port out of the URL if the port is the "default" for that protocol and whoever is processing the URL will automatically use the correct port. But, your server still must be listening on the proper port.

Like I want to add a app.route() would I be able to do that just in public_html?

I don't know what this means. You would not add routes in your HTML file. Adding routes would be done in a JS file on your server.

I run an Apache server on a shared host that uses cPanel.

You typically cannot run a node.js server that directly serves incoming connections from that type of hosting. You need hosting that supports long running server processes with routing of incoming connections to them. Your provider may offer that (at a different level of service) or you may need a different provider.

There are ways to use Apache as a proxy to route incoming connections to your node.js server, but that is typically not something you can configure yourself in a shared hosting environment. That would usually require a higher level of hosting service.

It is also possible use node.js as a batch execution environment from Apache (somewhat analogous to how PHP is run by Apache), but that is not running a node.js server. That's just running a node.js script from scratch each time a specific route is hit in Apache (so that's probably not what you want or were asking about).

You may find this a useful read: Running Node.js in apache?.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @CraigMitchell - Will your hosting redirect incoming port 80 requests made to your domain to your node.js server? That's what you need. And, will your hosting permit a long running node.js server? – jfriend00 Nov 22 '17 at 03:06
  • I noticed it said `EACCES 0.0.0.0:443` So it's saying the server is 0.0.0.0? Is this why this error occurs? – Craig Mitchell Nov 22 '17 at 03:47
  • @CraigMitchell - Did you attempt to [Google "EACCES nodejs. server"](https://stackoverflow.com/questions/9164915/node-js-eacces-error-when-listening-on-most-ports). If you did, you would have quickly found that it takes elevated privileges to start a server on any port below 1024 and this is a typical error one might get if they don't have the right privileges. – jfriend00 Nov 22 '17 at 04:57