简体   繁体   English

Node.js create-react-app构建

[英]Node.js create-react-app build

I am pretty new to web development and I was asked to create a single-page application with tools of my choice. 我是Web开发的新手,并被要求使用我选择的工具创建一个单页应用程序。 The only requirement is that it has to run locally on a GlassFish server (in NetBeans: Java Web > Web Application). 唯一的要求是它必须在GlassFish服务器上本地运行(在NetBeans中:Java Web> Web应用程序)。 I use the create-react-app starter kit provided by Facebook to build the application. 我使用Facebook提供的create-react-app入门套件来构建应用程序。 When I run npm run build I get a build folder containing an html-file and a js-file. 当我运行npm run build我得到一个包含html文件和js文件的构建文件夹。 But when I double-click the html-file, the browser opens and just shows an empty page. 但是,当我双击html文件时,浏览器将打开并仅显示一个空白页面。 Does anyone know what I have to configure in order to get a bundled html-file that shows the application when I open it? 有谁知道我必须配置什么才能获得一个捆绑的html文件,当我打开它时会显示该应用程序?

Thank you 谢谢

After running " npm run build " on your create-react-app generated code, it displays instructions to help with just this. 在您的create-react-app生成的代码上运行“ npm run build ”后,它会显示相关说明来帮助您。 It says something like: 它说像:

You may also serve it locally with a static server:

npm install -g pushstate-server
pushstate-server build

The first command, " npm install -g pushstate-server " only needs to be run once, as it installs "pushstate-server" to global NPM. 第一个命令“ npm install -g pushstate-server ”只需运行一次,因为它将“ pushstate-server”安装到全局NPM。 The second command " pushstate-server build " runs the simple static server, pushstate-server, and serves up any content in the "build" folder at http://localhost:9000 . 第二个命令“ pushstate-server build ”运行简单的静态服务器pushstate-server,并提供位于http:// localhost:9000的“ build”文件夹中的所有内容。 You can change the port number if you wish, by adding it at end of command line: " pushstate-server build 1234 " 您可以根据需要更改端口号,方法是在命令行末尾添加:“ pushstate-server build 1234

UDPATE: Serverless method... UDPATE:无服务器方法...

If your goal is to run the page from the file system, and not from a web server, you'll need to tweak the build/index.html to fix the paths to your JS and CSS (and less importantly, your favicon.ico). 如果您的目标是从文件系统而不是从Web服务器运行页面,则需要调整build/index.html以修复JS和CSS的路径(更不重要的是,您的favicon.ico )。 The index.html generated by create-react-app expects your JS and CSS to be at "/static/...". create-react-app生成的index.html期望您的JS和CSS位于“ / static / ...”。 However, when you open from the file system, that path is not correct. 但是,从文件系统打开时,该路径不正确。 If you remove the leading forward slash, making the URLs relative, your page will load properly from the file system: 如果删除前导斜杠(使URL相对),则页面将从文件系统正确加载:

After running "npm run build", open the generated "build/index.html" file. 运行“ npm run build”后,打开生成的“ build / index.html”文件。 Remove the leading forward slash from "/favicon.ico", "/static/js/main.[random string].js" and "/static/css/main.[random string].css" and save your changes (so that the all read "static/..." and not "/static/..."). 从“ /favicon.ico”,“ / static / js / main。[随机字符串] .js”和“ / static / css / main。[随机字符串] .css”中删除前导斜杠并保存更改(因此都读为“ static / ...”,而不是“ / static / ...”)。 Reload/refresh the page in the browser. 在浏览器中重新加载/刷新页面。

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

相关问题 我们使用 create-react-app 得到的应用程序可以称为 node.js 应用程序? - The application that we get using create-react-app can be called a node.js application? CERT_HAS_EXPIRED:使用create-react-app的node.js和https请求 - CERT_HAS_EXPIRED: node.js and https request using create-react-app create-react-app React Js 的漏洞 - Vulnerabilities with create-react-app React Js 部署 Node.js/React 应用程序时 Heroku 构建失败 - Heroku build failure when deploying Node.js/React app 当我们 create-react-app 时,BUILD DIR 中的 HTML 如何链接到 SRC DIR 中的 JS? - How HTML in BUILD DIR is linked to JS in SRC DIR when we create-react-app? 使用 create-react-app 构建开发/调试构建 - Build development/debug build with create-react-app 在使用 create-react-app 创建的 reactjs 应用程序中导入 node_module dist js 文件 - Import a node_module dist js file in reactjs application created using create-react-app React 路由在 facebook 的 create-react-app 构建中不起作用 - React routes are not working in facebook's create-react-app build React.js与ASP MVC与create-react-app的集成 - React.js integration with ASP MVC with create-react-app 将 Cesium/Resium 与 npx create-react-app 的构建集成不起作用 - Integrating Cesium/Resium with a build of npx create-react-app is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM