I have deployed an nodejs with express on cpanel with namecheap hosting. I have used the default provided service of create an nodejs app in cpanel. The problem is my GET requests are not working properly. I'm getting Cannot GET if I use "/" or "routename" for the route.
const express = require('express')
const app = express()
const cors = require('cors')
app.use(express.json())
app.use(
cors({
origin:'*'
})
)
app.get("/", (req, res) => {
console.log('req came')
res.send({ message: "Works!" });
});
app.listen('8080', ()=>{
console.log('Server started on port 8080')
})
But if I change the route to
app.get("*", (req, res) => {
console.log('req came')
res.send({ message: "Works!" });
});
by replacing the "/" by " * "(ignore the space around the asterisk as it wasn't rendered without the spaces) it works. But the problem is since "*" redirects to every request I can't use another route. I have also tried removing the port and using just
app.listen()
but it's the same result. What could be causing this issue? Maybe something wrong with the passenger file? I also tried adding the following on top of the .htaccess according to the suggestion on NodeJS '/' home route is working but the remaining routes are not working (CPanel Shared Hosting) but no positive result.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^server/(.*)?$ https://127.0.0.1:8080/$1 [P,L]
Nodejs version : 10.24.1
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.18.1",
Already contacted Namecheap support for this problem. They mentioned that the application heavily relies on the asterisk to work. Not sure if that's a case.
I found the solution thanks to this resouce How to fix the Node.js error: “Cannot GET” URL
the problem was I had to use the app directory name as the base url. So it would be like
app.get("/appdirectoryname/", (req, res) => {
console.log('req came')
res.send({ message: "Works!" });
});
for using a different route just appending that to the base route name works. For example
app.get("/appdirectoryname/routename", (req, res) => {
console.log('req came')
res.send({ message: "Works!" });
});