当我使用 npm start
在本地运行 React.js 前端时,它运行良好。当我尝试使用 npm install
然后使用 nom start
在服务器中运行它时,会出现空白页面。
我检查了 public
文件夹,它在 index.html
中有以下内容:
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
integrity="sha384-akgkshgfs"
crossorigin="anonymous">
</head>
<div id="root"></div>
在package.json
中,我使用http-server -a XXX.XXX.XXX.XXX -p 55000
:
"scripts": {
"start": "http-server -a XXX.XXX.XXX.XXX -p 55000",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
当我在本地使用时:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
它在localhost:3000
中运行良好,但在服务器中,XXX.XXX.XXX.XXX:55000
中的页面为空白。
看起来我错过了一些安装步骤。
有什么想法吗? (这是我第一次使用 React.js 和 Node)。
最佳答案
http-server
如果未指定任何内容,则使用 ./public
作为网站的默认入口点 ( docs )。
正如您在评论中提到的,您在服务器上构建项目,因此捆绑的项目位于 /build
内。所以http-server没有内容可显示。
要显示该站点,您必须更改启动脚本
"start": "http-server -a XXX.XXX.XXX.XXX -p 55000",
至
"start": "http-server /build -a XXX.XXX.XXX.XXX -p 55000",
--
正如评论中提到的,最好在本地构建生产站点并仅部署捆绑的代码。 请参阅以下文章,了解有关更好部署的更多信息:
关于javascript - React.js 应用程序在服务器中作为空白页面打开,而在本地运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54763852/