gpt4 book ai didi

google-app-engine - 我如何在 GAE 中设置路由以使用react?在基本 create-react-app 的 GAE 中,通过 URL 直接路由到 react-router-dom 路由失败?

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:37 26 4
gpt4 key购买 nike

现在回答

这对我来说很难完全正确。通过谷歌的指导方式很少。我希望这对其他人有帮助。

正如 Dan Cornilescu 指出的那样,训练员接受第一场比赛。所以我们从更具体到不太具体。我已经按照 npm run build 创建的文件夹结构解决了这个问题:1.处理js、css、media的子目录。2.处理manifest.json和fav.ico的json和ico请求。3. 将所有其他流量路由到 index.html。

handlers:
- url: /static/js/(.*)
static_files: build/static/js/\1
upload: build/static/js/(.*)
- url: /static/css/(.*)
static_files: build/static/css/\1
upload: build/static/css/(.*)
- url: /static/media/(.*)
static_files: build/static/media/\1
upload: build/static/media/(.*)
- url: /(.*\.(json|ico))$
static_files: build/\1
upload: build/.*\.(json|ico)$
- url: /
static_files: build/index.html
upload: build/index.html
- url: /.*
static_files: build/index.html
upload: build/index.html

欢迎更高效的回答。

Original Question:

Setting GAE app.yaml for react-router routes produces "Unexpected token <" errors.

在开发环境中,所有路由在直接调用时都有效。 localhost:5000 和 localhost:5000/test 产生预期结果。

在 GAE 标准 app.yaml 中,当直接调用 URL www.test-app.com 时,根目录的函数。 www.test-app.com/test 产生 404 错误。

app.yaml #1

runtime: nodejs8
instance_class: F1
automatic_scaling:
max_instances: 1

handlers:
- url: /
static_files: build/index.html
upload: build/index.html

配置 app.yaml 以接受所有路径的通配符路由失败。 www.test-app.com/和 www.test-app.com/test 产生错误“Unexpected token <”。它似乎正在将 .js 文件作为 index.html 提供。

app.yaml #2

runtime: nodejs8
instance_class: F1
automatic_scaling:
max_instances: 1

handlers:
- url: /.*
static_files: build/index.html
upload: build/index.html

重现此问题的步骤:

节点:10.15.0npm: 6.4.1

gcloud init 通过 Google Cloud SDK

npm init react-app test-app

npm install react-router-dom

将路由器添加到 index.js:

index.js

import {BrowserRouter as Router, Route} from 'react-router-dom';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';


ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root'));
serviceWorker.unregister();

将路由添加到 app.js:

app.js

import {Route} from 'react-router-dom'
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
render() {
return (
<div>
<Route exact path="/"
render={() => <div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>} />
<Route exact path="/test"
render={() => <div className="App">
Hello, World!
</div>} />
</div>
);
}
}

export default App;

package.json 没有变化:

package.json

{
"name": "test-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}

npm 运行构建

gcloud 应用部署

我们如何说服应用引擎允许 React SPA 直接处理路由?

最佳答案

我从 Nathan Shephard 中得到了答案和 Dan Cornilescu并将它们压缩到下面的 app.yaml 中。它似乎适用于我在 GAE 标准上的 React SPA。不需要应用程序服务器(例如:serve.js、ExpressJS 等)。

env: standard
runtime: nodejs10
service: default

handlers:
- url: /static
static_dir: build/static

- url: /(.*\.(json|ico|js))$
static_files: build/\1
upload: build/.*\.(json|ico|js)$

- url: .*
static_files: build/index.html
upload: build/index.html

关于google-app-engine - 我如何在 GAE 中设置路由以使用react?在基本 create-react-app 的 GAE 中,通过 URL 直接路由到 react-router-dom 路由失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54247953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com