在netlify中部署react应用程序的问题

问题描述 投票:0回答:1
  • 我正在尝试在netlify中部署反应应用程序,如中间链接:https://link.medium.com/nw9ZCh0lrV所述
  • 所以我使用快速服务器来定义构建脚本并在生产模式下设置应用程序
  • 构建脚本在本地计算机中创建并上载到netlify站点
  • 它将消息显示为成功部署
  • 单击URL链接时,它将构建脚本的GET请求状态显示为404
  • netlify链接URL:https://nostalgic-euclid-4f95ab.netlify.com
  • 你们可以帮我解决你的建议吗?
  • 下面附带的应用程序截图 - 提供下面的代码片段:

server.js

const express = require('express');
const favicon = require('express-favicon');
const app = express();

app.use(favicon(__dirname + '/build/favicon.ico'));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname,'build')));
app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

的package.json

"scripts" : {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

enter image description here enter image description here

javascript reactjs npm create-react-app netlify
1个回答
1
投票

create react app中的常见错误是在homepage文件中添加与您的站点位置不匹配的package.json。完成生产构建后,资产将以域之后的任何路径的值为前缀。

https://nostalgic-euclid-4f95ab.netlify.com/codehangar/react-interview/static/js/main.7ab6795d.chunk.js

从损坏的(404)资产的路径看,您的主页值看起来像:

  "homepage": "https://example.com/codehangar/react-interview",

如果要包含主页值,请确保它与您的网站网址相同。

在您的情况下,值应为:

  "homepage": " https://nostalgic-euclid-4f95ab.netlify.com",

注意:*通常情况下,请将此设置保留在文件之外,直到您处于活动状态并设置域名为止。

也:

  • 无需将构建文件夹存储在存储库(.gitignore)中。 build命令将在每次部署时替换它。
  • 您不需要提供的server.js文件。
© www.soinside.com 2019 - 2024. All rights reserved.