当我在根 html 文件中使用 create-react-app 的%PUBLIC_URL%令牌时,WebStorm 抱怨它无法解析它所属的 url。
错误是:无法解析目录“%PUBLIC_URL”
在这种情况下,警告是虚假的:它在开发和生产中按预期工作。该%PUBLIC_URL%令牌映射到目录的位置public/,我已将其标记为资源根。
我可以使用指令抑制整个文件的警告<!--suppress HtmlUnknownTarget -->,但这意味着 WebStorm 不会检查 url 是否有效。该注释也会出现在最终的 html 输出中,这也是不可取的。
我可以让 WebStorm 理解%PUBLIC_URL%create-react-app 实例上下文中的含义吗?
前端和后端两种不同的技术,到目前为止都很好。
问题(反过来,这是最常见的情况)是前端和后端都必须从同一域(或服务器/文件结构)提供服务。
使两个项目共存于同一服务器的最佳方法是什么?
编辑:这里的主要问题是,由于 create-react-app 堆栈,Laravel Mix 和任何 Laravel 前端功能都无法使用。它所施加的规则使得在不首先弹出应用程序的情况下进行集成变得非常困难,从维护的角度来看,这是不推荐的。
我是 React 新手,我将部署一个 React 项目。React项目由create-react-app创建,然后生产代码由“npm build”构建,并由Express托管。
在项目中,有一些对 API 服务器的 fetch 调用,其中的 URL 是可配置的。在开发中,我可以通过在文件 .env 的环境变量中设置它(例如 REACT_APP_API_URL= http://somewhere/)并在代码中导入来完成此操作。
然而,似乎这些环境变量在运行“npm build”后变得静态,我无法再更改它,即使在启动服务器时执行类似“REACT_APP_API_URL= http://otherserver/ node express_server.js”的操作也是如此。
我想问是否有任何方法可以在“npm build”之后为代码获取一些配置,如果可以从文件或.env获取是最好的,从环境变量对我来说也可以。
谢谢。
我正在尝试设置og-imageCRA 应用程序,我正在这样做:
<meta content="website" property="og:type" />
<meta content="%PUBLIC_URL%/og-image.png" property="og:image" />
<meta content="Tile goes here..." property="og:title" />
图像位于pubic文件夹内,但图像未显示在操作上。
当我这样做时http://localhost:3000/og-images.png,它起作用了。但是,我不想在这里设置固定值。
我做错了什么?
编辑:我的文件夹结构:
app
- public
- index.html
- favicon.ico
- og-image.png
- src
Run Code Online (Sandbox Code Playgroud) 我有一个旧的 Angular 应用程序和一个新的 React 应用程序,我想通过 nginx 公开它们。
我有一个包含静态文件的生产 nginx 配置,但我试图让开发人员也能工作 - 使用热重载和 webpack 开发服务器。我已经按照它应该的方式工作了,但我意识到如果我可以使用 /r/ 来确定反应范围,并使用 /a/ 来确定角度应用程序,那就更好了。但是,使用下面的配置,每当我访问 时localhost/r/,bundle.js 都会位于 中/static/。有没有办法制作/static/反应/r/static/应用程序?
有更好的方法吗?
location /r {
proxy_pass http://172.17.0.2:3000;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /static/ {
proxy_pass http://172.17.0.2:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /sockjs-node/ {
proxy_pass http://172.17.0.2:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade …Run Code Online (Sandbox Code Playgroud) 我有一个使用create-react-app创建的 React 应用程序,我已将其部署到 Azure Web 应用程序。没有后端:该网站是纯粹的“静态”。
在生产环境中,我需要保证 API 服务的许多密钥和其他机密的安全,但客户端应用程序需要能够读取它们。
在 React 中,有一种使用文件访问特定于环境的信息的机制.env,例如.env.production,但这不适合保密,因为代码中提到的环境变量.env在构建过程中被替换为文件中的实际值,因此任何在浏览器中查看 JavaScript 的人都可以看到。
设置 AppSettings 的值可以在 Azure 门户上完成(或通过 CI/CD 管道中的合适脚本),但如何在运行时读取 AppSettings 值?
许多 StackOverflow 问题都与此相关,但没有一个答案正确解决了基本问题,或者似乎没有抓住要点。例如,这里和这里。
appsettings azure reactjs azure-web-app-service create-react-app
我目前正在升级普通 React 应用程序以使用 Next.js(版本 7.0.0)。该应用程序最初是使用 Create-React-App 构建的,并利用 CRA 服务器内置的代理。
在开发过程中,我的 React 应用程序在端口 3000上运行,我有一个 Express 服务器在端口 5000上运行。在添加 Next.js 之前,我一直在package.json文件中使用代理对象将 API 请求路由到服务器。
API请求:
const res = await axios.get('/api/request')
Run Code Online (Sandbox Code Playgroud)
package.json 中的代理对象:
"proxy": {
"/api/*": {
"target": "http://localhost:5000"
}
}
Run Code Online (Sandbox Code Playgroud)
这一直工作得很好,但使用 Next.js 我现在收到一个错误:
GET http://localhost:3000/api/request 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)
^ 这应该指向 locahost:5000 (我的服务器)
有谁知道我如何能够将 API 请求从 React/Next.js 客户端路由到在不同端口上运行的 Express 服务器?
我使用create-react-app创建一个应用程序,并在尝试构建生产优化版本时遇到错误yarn build:
> yarn build help
yarn run v1.9.4
$ react-scripts build help
Creating an optimized production build...
Failed to compile.
Expected a pseudo-class or pseudo-element.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不是一个非常有用的错误。谷歌搜索似乎找到了有关postcss的内容,但我不确定它与 create-react-app 有何关系,或者如何解决该问题。
以前有人遇到过类似的问题吗?
请注意,该应用程序在开发模式下编译良好:
Compiled successfully!
You can now view note-app in the browser.
Local: http://localhost:3000/
On Your Network: http://192.168.0.54:3000/
Note that the development build is not optimized.
To create a production build, …Run Code Online (Sandbox Code Playgroud) 我已经使用 create-react-app 创建了一个反应应用程序,并且我正在尝试使用酶对简单组件进行简单测试。
标头.test.js:
import Header from './Header';
import { shallow } from 'enzyme';
describe('Header component', () => {
it('renders correctly', () => {
const wrapper = shallow(<Header />);
expect(wrapper.exists()).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,我不断收到此错误:
Cannot find module 'entities/maps/xml.json' from 'Tokenizer.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
at Object.<anonymous> (node_modules/cheerio/node_modules/htmlparser2/lib/Tokenizer.js:6:17)
Run Code Online (Sandbox Code Playgroud)
我的预感是这与 webpack 有关,但我不确定......救命!
哦,是的,这是我的 package.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.8",
"@fortawesome/free-solid-svg-icons": "^5.5.0",
"@fortawesome/react-fontawesome": "^0.1.3",
"bootstrap": "^4.1.3",
"http-proxy-middleware": "^0.19.0",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-redux": "^5.1.1",
"react-scripts": "2.1.1",
"react-test-renderer": "^16.6.1",
"reactstrap": …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Ubuntu 18.10 上使用 create-react-app 创建一个新的 ReactJS 应用程序。当我运行时,create-react-app myapp我收到以下注释:
注意:该项目是使用不受支持的旧版本工具启动的。请更新到 Node >=6 和 npm >=3 以在新项目中获取支持的工具。
但是当我检查我的节点版本时,node -v我得到 v10.13.0 并检查 npmnpm -v显示 6.4.1。create-react-app --version显示 2.1.1。所以我的 Node 版本和 npm 版本都大于指定的版本。
我不知道该怎么办,因为我的版本已经高于指定的版本。对于如何解决这个问题,有任何的建议吗?