简体   繁体   English

无法获取 cpanel 上的 Nodejs Express 应用程序。 但在 GET 请求中使用 * 时有效

[英]Cannot GET for Nodejs Express app on cpanel. But works when * is used in GET request

I have deployed an nodejs with express on cpanel with namecheap hosting.我已经在带有 namecheap 托管的 cpanel 上部署了一个带有 express 的 nodejs。 I have used the default provided service of create an nodejs app in cpanel.我使用了默认提供的在 cpanel 中创建 nodejs 应用程序的服务。 The problem is my GET requests are not working properly.问题是我的 GET 请求无法正常工作。 I'm getting Cannot GET if I use "/" or "routename" for the route.如果我对路线使用“/”或“routename”,我将无法获取。

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.我还尝试根据 NodeJS '/' 主路由的建议在 .htaccess 之上添加以下内容,但其余路由不起作用(CPanel 共享主机)但没有积极的结果。

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 Nodejs 版本:10.24.1

"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.18.1",

Already contacted Namecheap support for this problem.已就此问题联系 Namecheap 支持。 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 resouceHow to fix the Node.js error: “Cannot GET” URL由于这个资源,我找到了解决方案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.问题是我必须使用应用程序目录名称作为基础 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!" });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM