在react - heroku中生成生产构建全栈应用程序

hs1ihplo  于 2022-11-13  发布在  React
关注(0)|答案(1)|浏览(109)

我在react中开发了全栈应用。我使用heroku部署了我的应用,应用的前端无法工作https://jovelapp2.herokuapp.com-消息“Cannot get /”https://jovelapp2.herokuapp.com/api/persons-但当我运行命令“node index.js”(整个应用)时,我的后端路由工作正常
我运行了命令“npm run build”,然后将build文件夹复制到我的后端项目中,我在本地测试了应用程序,并运行了命令节点index.js,我本地机器上的一切都工作正常...
这是我从日志中得到的:

2022-11-05T20:26:20.838620+00:00 app[web.1]: GET /api/persons {}
2022-11-05T20:26:20.838656+00:00 app[web.1]: GET /api/persons 200 223 - 0.296 ms
2022-11-05T20:28:37.129945+00:00 app[web.1]: GET /api/presons {}
2022-11-05T20:28:37.129966+00:00 app[web.1]: GET /api/presons 404 150 - 0.186 ms
2022-11-05T20:28:37.128672+00:00 heroku[router]: at=info method=GET path="/api/presons" host=jovelapp2.herokuapp.com request_id=9b2d2299-4d6f-468f-beb1-3505041277ee fwd="77.137.65.235" dyno=web.1 connect=0ms service=1ms status=404 bytes=426 protocol=https
2022-11-05T20:28:39.432076+00:00 heroku[router]: at=info method=GET path="/" host=jovelapp2.herokuapp.com request_id=00f7b3bd-fb30-4dfd-a38d-d8cc41308bb1 fwd="77.137.65.235" dyno=web.1 connect=0ms service=1ms status=404 bytes=415 protocol=https
2022-11-05T20:28:39.433257+00:00 app[web.1]: GET / {}
2022-11-05T20:28:39.433278+00:00 app[web.1]: GET / 404 139 - 0.169 ms

请告诉我,如果你需要一些从我的代码更好地理解它。
提前感谢!

g9icjywg

g9icjywg1#

您的项目似乎已成功部署。
如果要处理路径“/”中的GET请求,则需要显式地为其创建一个处理程序。
看起来像Express.js,所以类似下面这样的代码应该可以完成这项工作:

const app = express();
app.get('/', (req, res) => {
    res.send('this is the root path!')
});

相关问题