Hide port for nodeJS app using App Manager

pharmHalo

Registered
Oct 19, 2021
3
0
1
australia
cPanel Access Level
Root Administrator
Hi everyone,

Please bare with me as I'm quite new to developing but have 'made' my first API and now need to get it working more securely.

Target:
To get my api to be accessed via www.MyWebsite.com/myAPI instead of mywebsiteapi:3000

I will throw some details down and be as succinct as possible and if someone could please point me in the right direciton it would be amazing.

  • I have created an API running on Node JS
  • I used "Visual Studio Code" and connected Via ssh.
  • The main app runs on port 3000
    • It is possible that my main app file is incorrectly named to use Application manager to do what I need (see below)
  • I am running my own VPS with a company assisting in management.
  • On NodeJS i am starting it and stopping it using the SSH terminal in VS Code.
  • I am running "FOREVER" to keep the service running when terminal is closed and also to restart it if there are errors
  • I have created an application in CPANEL application manager.
    • The Base app URL is mywebsite.com.au/myapi
    • The app path is /myAppPath


When I test this with Postman the port 3000 reference call will work, but when I try the /myapi call it doesnt work, there is no response, just a continued 'waiting for response'


Where do i look now to figure this out please because I want to get this working :(
 

pharmHalo

Registered
Oct 19, 2021
3
0
1
australia
cPanel Access Level
Root Administrator
I have tried a few new things today:
  • Created a copy of the APi under a different folder 'test'
  • made this port 3001 without using the FOREVER app


I'm now getting into the app because I have cleaned up my app.js section but my routes are not working, all are coming up as "endpoint not found" using this as a 404 error response.

Once again, if I use the port naming convention then this all works.

This is how i am "listening":

Code:
const port = process.env.port || 3001;
const schedule = require('node-schedule');
const stripePayments = require('./api/routes/stripePayments');
const http = require('http');


const app = express();

app.use((req,res,next)=>{
    res.header("Access-Control-Allow-Origin","*");
    res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept,Authorization");

    if (req.method === 'OPTIONS'){
        res.header('Access-Control-Allow-Methods','PUT,POST,PATCH,DELETE,GET');
        return res.status(200).json({});
    }
    next();
});

//Routes (deleted except one)
const chemistRoutes = require('./api/routes/chemists');

app.use('/chemists',chemistRoutes);

app.use((req,res,next) => {
    const error = new Error('Endpoint not Found');
    error.status = 404;
    next(error);
})

app.use((error,req,res,next) => {
res.status(error.status || 500);
res.json({
    error: {
        message: error.message
    }
    });

});
var server = http.Server(app);

server.listen({port}, function(err){

});
 

cPanelAnthony

Administrator
Staff member
Oct 18, 2021
583
54
103
Houston, TX
cPanel Access Level
Root Administrator
Hello! Regarding whether or not you can obscure the port number in this way, this wouldn't really be possible; at least not via any common methods that cPanel would utilize. It might be possible to do this via a rewrite proxy rule. However, you would have to get the conditions perfect and there would be a lot of room for error. I cannot guarantee this would work the way you need it to.


Perhaps other users here will have useful advice. Best of luck getting the API functioning in the way you need!
 

pharmHalo

Registered
Oct 19, 2021
3
0
1
australia
cPanel Access Level
Root Administrator
Hi Anthony.,

Sorry my terminology probably wasnt correct.

The idea is that I'm using the application manager so that my API calls will call the URL /myAPI instead of calling the port,

This means i can close the port on iptables and reduce external exposure to the ports and just leave it as a URl call.


So inreality everything is almost working with the exception of the routes arent being "seen" using the application manager path.
 

cPanelAnthony

Administrator
Staff member
Oct 18, 2021
583
54
103
Houston, TX
cPanel Access Level
Root Administrator
Thank you for the confirmation! From my research, I am not finding a way to completely avoid the use of a port with a node.js app. I would highly recommend reaching out to a systems administrator if available to help determine if a solution like this would be possible. Otherwise, maybe other users on this forum will have some advice for you. Thank you for your understanding.