NodeJS POST not working when using Application manager (changes to GET!)

pharmHalo

Member
Oct 19, 2021
10
3
3
australia
cPanel Access Level
Root Administrator
Hi all,
Very weird but I have a working NodeJS API consisting of:
app.js
route folders
- Route.js files

I have used application manager to assign this app to /myAPI

The app is starting and running.

The GET requests all work.

The weirdset thing is that the POST request is actually being CHANGED to a get request!!!?

If i run the api via:
mydomain.com:3001 the Post request works.
If I use
mydomain.com/myAPI then the POST request is rejected as the enpoint cannot be found. I have checked the terminal output and it is being asked a GET instead of post.

The original C# code doesnt change at all making the request, just the domain name /port.

Has anyone else seen this kind of issue??


In C# I'm using "Method.POST"

API info:

File app.js:

Code:
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();
});




const userRoutes = require('./api/routes/user');


app.use('/myAPI/user',userRoutes);
file user.js

Code:
router.post('/profileSettings/',verify,(req,res) => {
    var sqlString = "SET @userID = ?;\
        CALL updateuser(@userID);";
    
    
    try{
        connection.query(sqlString,[req.body.user],(err,rows,fields)=>{
            //console.log(req.user._id);
            if(!err)
            {
                return res.status(200).json({
                    Result: true
                    
                });           
            }
            else
            //console.log(err),
                return res.status(460).json({
                    Result: true
                    
                });         
        })
    } catch (e) {
        // this catches any exception in this scope or await rejection
        console.log(e);
        res.status(500).json({ Result: e });
    }
        
    
    
});
As mentioned, if i do it with port, it works and log shows POST
if I use the app manager URl then this errors out and the log says i sent a GET request!!?

Thanks for any ideas
 
  • Like
Reactions: cPanelAnthony

pharmHalo

Member
Oct 19, 2021
10
3
3
australia
cPanel Access Level
Root Administrator
Hi Anthony,
I thought to write on here that I solved this issue...
I dont know WHY it solved it but the POST would work as a POST but only when i usedHTTPS.

If i used HTTP then the post would change to a get on the receiving end in the API!!

CHanged all calls to HTTPS and the issue is now gone..
 
  • Like
Reactions: cPRex