【问题标题】:Browser on Google Cloud Platform谷歌云平台上的浏览器
【发布时间】:2021-04-23 15:31:12
【问题描述】:

我对使用 Google Cloud Platform 并在 GCP 上创建 VM 实例比较陌生。

有人可以建议如何在此实例上设置浏览器。设置实例时找不到浏览器选项。

请注意,一个 React 应用程序正在此 VM 实例上运行,因此希望通过 http://localhost:3000 访问它。

我尝试的一种方法是允许 VM 实例上的传入流量,然后从本地计算机浏览器访问上述 URL,但这不起作用,因为以下是本地计算机浏览器中的错误:

跨源请求被阻止:同源策略不允许读取 URL 处的远程资源。 (原因:CORS 请求没有成功)

另一种方法我尝试在 VM 实例本身上使用命令 curl localhost:3000,但其输出如下:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <!--
      Notice the use of  in the tag above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.
      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.
      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.
      To begin the development, run `npm start`.
      To create a production bundle, use `npm run build`.
    -->
  <script type="text/javascript" src="/static/js/bundle.js"></script></body>
</html>

如果浏览器安装方式不正确,那么如何测试在 GCP VM 实例上运行的 React 应用程序?

【问题讨论】:

  • 您好。也许值得添加有关您的环境的更多信息。就像您的虚拟机运行的是 Windows 或 Linux,您打算将应用程序公开到 Internet 还是仅在内部?在我看来,您可以应用许多解决方案,但我们需要更多地了解您的环境。
  • GCP VM 实例是 Linux instance-2 4.19.0-16-cloud-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 运行 React 应用程序的 GNU/Linux在 3000 端口。现在想通过访问 URL 来看看它的样子。

标签: javascript reactjs google-cloud-platform browser


【解决方案1】:

除非您使用的是非图形版本的 windows 服务器,并且考虑到所有 windows 服务器都默认至少带有可用的 Internet Explorer,否则我会假设您正在使用 linux CLI 机器(同样,图形版本的Linux 通常默认自带 Mozilla 或 Opera 浏览器)。

正如 Armando 在上面的评论中提到的那样,有必要知道您的应用是否暴露在互联网上。如果是这种情况,则无需在您的 VM 上设置或安装任何浏览器,您只需要获取您的 VM 外部 IP 和您的应用程序端口,然后以您粘贴的相同格式将其粘贴到任何浏览器(例如 chrome)中上面但是用这样的外部IP替换“localhost”。

为此,您当然需要通过转到您的GCP Console >> Compute Engine >> VM Instances 来确保您的实例允许 http 和 https 流量。在 VM Instances 页面上,单击您的实例名称以查看详细信息,查找“Firewall”部分并查看复选框是否标记为“Allow HTTP Traffic”和“Allow HTTPS Traffic”。

如果未标记,但该页面仍会暴露在互联网上,在同一页面中,标题“VM 实例详细信息”旁边有一个编辑按钮,可让您启用此类复选标记。

第二个选项是启用 VNC 以获取服务器的图形会话,以在 Debian 发行版的下一篇文章中实现它:

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-debian-9

或者如果你是 CetOs/RHEL 发行版的话,这个:

https://www.linuxtechi.com/install-configure-vnc-server-centos-7-rhel-7/

或者第三种简单的选择是安装 lynx 之类的第 3 方软件来浏览网站:

sudo apt install lynx

安装后,只需输入“lynx”,后跟您的网址,即:

lynx google.com

它会显示谷歌搜索的命令行版本:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 2020-06-02
    • 2014-12-04
    • 2020-09-04
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2018-09-23
    相关资源
    最近更新 更多