简体   繁体   English

在 AWS Amplify 中重写和重定向

[英]Rewrites and redirects in AWS amplify

I am new to AWS deployment and trying to deploy both Backends ( NodeJS, Express, MongoDB ) and Frontend( React/Redux ) code to AWS.我是AWS deployment新手,并尝试将后端( NodeJS, Express, MongoDB )和前端( React/Redux )代码部署到 AWS。 I am using AWS Beanstalk for backend and AWS amplify for Frontend code.我使用AWS Beanstalk作为后端,使用AWS amplify作为前端代码。 I have completed the backend deployment and tested it using postman and even testing the API's by running frontend at http://localhost:3000/ .我已经完成了后端部署并使用邮递员对其进行了测试,甚至通过在http://localhost:3000/运行前端来测试 API。 I have deployed the code on AWS amplify also but seems I am not aware of the Rewrites and redirects it uses so that the app can navigate to different URL's.我也在 AWS 放大上部署了代码,但似乎我不知道它使用的Rewrites and redirects ,以便应用程序可以导航到不同的 URL。 Mine is a SPA .我的是SPA I can see the login screen but once I enter the credentials it dosen't navigate to protected(Using JWT Auth ) URL.我可以看到登录屏幕,但是一旦我输入凭据,它就不会导航到受保护的(使用JWT Auth身份JWT Auth )URL。

My app on amplify is hosted on : https://url-resolve-changes.d1z993fyobkz3s.amplifyapp.com/我在 amplify 上的应用托管在: https://url-resolve-changes.d1z993fyobkz3s.amplifyapp.com/ : https://url-resolve-changes.d1z993fyobkz3s.amplifyapp.com/

App.js -> Contains different routes. App.js -> 包含不同的路由。 Apart from /login and /register all the routes have to be checked first using PrivateRoute and then can render the Homepage.除了/login/register所有路由都必须首先使用 PrivateRoute 检查,然后才能呈现主页。 I declared BASE_URL = host but I am not sure how I can use it that to navigate on different URL's我声明了 BASE_URL = host 但我不确定如何使用它来导航不同的 URL

// const BASE_URL = "https://url-resolve-changes.d1z993fyobkz3s.amplifyapp.com";

class App extends Component {
  render() {
    return (
      <div className="App">
        <MessageSnackbar />
        <Router history={history}>
          <Switch>
            <PrivateRoute
              exact
              path={`/`}
              renderFunc={routeProps => <HomePage {...routeProps} />}
            />
            <Route path={`/login`} component={Login} />
            <Route path={`/register`} component={Register} />
          </Switch>
        </Router>
      </div>
    );
  }
}

export default withTheme()(App);

I have below configured in Rewrites and redirects in the Amplify settings.我在 Amplify 设置中的重写和重定向中进行了以下配置。

在此处输入图片说明

manifest.json清单文件

{
  "short_name": "React App",
  "name": "Create React App Sample",
  "icons": [
    {
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
    }
  ],
  "start_url": ".",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"
}

The issue is that nothing loads as of now when I hit :问题是,当我点击时,目前没有任何负载:

https://url-resolve-changes.d1z993fyobkz3s.amplifyapp.com/index.html

OR要么

https://url-resolve-changes.d1z993fyobkz3s.amplifyapp.com/

The backend URL though is correctly configured as when I send the request from http://localhost:3000/ the API's are working fine.当我从http://localhost:3000/发送请求时,后端 URL 已正确配置,API 工作正常。 It's just the redirecting part I am missing.这只是我缺少的重定向部分。 Do let me know if any other details are required.如果需要任何其他详细信息,请告诉我。 Appreciate your help.感谢您的帮助。 Thanks in advance.提前致谢。

While I reshared your question on https://twitter.com/nswebstudio/status/1149276084304613376 I figured it out its amplify.yml issue.虽然我在https://twitter.com/nswebstudio/status/1149276084304613376上转发了您的问题,但我发现它是 amplify.yml 问题。 I corrected my Build path as follows我更正了我的构建路径如下

baseDirectory: build

Complete amplify.yml file screenshot完整的 amplify.yml 文件截图在此处输入图片说明

Also read for reference: https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html另请阅读以供参考: https : //docs.aws.amazon.com/amplify/latest/userguide/build-settings.html

Hope it helps希望能帮助到你

I had a problem when refreshing page with route path, received response AccessDenied , caused by S3 does not really have that path or file.使用路由路径刷新页面时遇到问题,收到响应 AccessDenied ,由 S3 引起的实际上没有该路径或文件。 Got this question first on search result, so posting my solution here.首先在搜索结果上遇到这个问题,所以在这里发布我的解决方案。

Resolved the issue by:通过以下方式解决了问题

Original address:原地址:

</^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|woff2|ttf|map|json)$)([^.]+$)/>

在 Amplify 设置中的重写和重定向中配置:

Redirects for single page web apps (SPA) 单页 Web 应用程序 (SPA) 的重定向

Most SPA frameworks support HTML5 history.pushState() to change browser location without triggering a server request.大多数 SPA 框架都支持 HTML5 history.pushState() 在不触发服务器请求的情况下更改浏览器位置。 This works for users who begin their journey from the root (or /index.html), but fails for users who navigate directly to any other page.这适用于从根目录(或 /index.html)开始旅程的用户,但不适用于直接导航到任何其他页面的用户。 Using regular expressions, the following example sets up a 200 rewrite for all files to index.html except for the specific file extensions specified in the regular expression.使用正则表达式,以下示例将所有文件设置为 index.html 的 200 次重写,但正则表达式中指定的特定文件扩展名除外。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM