简体   繁体   English

将应用程序部署到谷歌应用程序引擎并希望启动 api 和客户端

[英]Deploying app to google app engine and want to start api and client

Following this tutorial to get an api and client on google cloud platform:按照本教程在谷歌云平台上获取 api 和客户端:

https://www.freecodecamp.org/news/create-a-react-frontend-a-node-express-backend-and-connect-them-together-c5798926047c/ https://www.freecodecamp.org/news/create-a-react-frontend-a-node-express-backend-and-connect-them-together-c5798926047c/

I have a root dir with an /api and /client inside it我有一个根目录,里面有 /api 和 /client

the /api package.json has the following script /api package.json 具有以下脚本

"scripts": {
    "start": "node ./bin/www"
},

the /client package.json has the following script /client package.json 具有以下脚本

"scripts": {
    "client-install": "npm install --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
},

When I try to deploy it says:当我尝试部署时,它说:

Step #0: Application detection failed: Error: node.js checker: Neither "start" in the "scripts" section of "package.json" nor the "server.js" file were found.步骤#0:应用程序检测失败:错误:node.js 检查器:“package.json”的“scripts”部分中的“start”和“server.js”文件均未找到。 Finished Step #0完成步骤#0

I'm thinking it can't find the scripts?我在想它找不到脚本? What is the best approach to start my api and client at the same time when I deploy?在部署时同时启动 api 和客户端的最佳方法是什么?

Well in that tutorial that guy creates the react app using create-react-app and because of that I don't know why you have in the 'start' command in your second package.json the following command 'node server.js'.好吧,在那篇教程中,那个人使用 create-react-app 创建了 react 应用程序,因此我不知道为什么您在第二个 package.json 中的“启动”命令中有以下命令“node server.js”。 You should have "start": "react-scripts start", that won't fix your problem but I'm not sure if server.js another server or what.你应该有“start”:“react-scripts start”,这不会解决你的问题,但我不确定 server.js 是另一个服务器还是什么。

Anyway I'll try to help you, first 'create-react-app' creates an app which internally uses webpack-dev-server which is cool for developing, but in order to deploy your app you need to run this command无论如何我会尽力帮助你,首先'create-react-app'创建一个内部使用webpack-dev-server的应用程序,这对于开发来说很酷,但是为了部署你的应用程序,你需要运行这个命令

npm run build

After that you will have a folder called 'build' with your react app.之后,您的 React 应用程序将拥有一个名为“build”的文件夹。 Then you need to copy that folder and go to your server project and paste the 'build' folder there.然后您需要将该文件夹和 go 复制到您的服务器项目并将“构建”文件夹粘贴到那里。 Once you finish that you need to add the following code to tell your server that you want to serve static files完成后,您需要添加以下代码来告诉您的服务器您要提供 static 文件

app.use(express.static(path.join(__dirname, 'build')));

And you also need to add the route而且你还需要添加路线

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

Now you can test it locally running only your server and the '/' path should take you to the react app.现在您可以仅在本地运行您的服务器对其进行测试,并且“/”路径应该将您带到 react 应用程序。 Once you know that is working, you can deploy ONLY that server with the build folder and just one package.json to google app engine.一旦你知道它工作正常,你就可以只部署该服务器与构建文件夹和一个 package.json 到谷歌应用程序引擎。 I hope helped you!希望对你有帮助!

My supposition is that the reported problem is generated by the fact that in root directory there is no package.json file.我的假设是,报告的问题是由于根目录中没有package.json文件而产生的。 You could try to add one in root directory to handle stuff in app and client directories, but...您可以尝试在根目录中添加一个来处理应用程序客户端目录中的内容,但是...

... but a taken a look to the tutorial you used and I found some things I don't like, so you want to give you some suggestions. ...但是看了你使用的教程,我发现了一些我不喜欢的东西,所以你想给你一些建议。 I recently developed a simple tool to visualize charts about covid-19 diffusion in Italy, it uses same structure as your app with different approaches.我最近开发了一个简单的工具来可视化有关意大利 covid-19 扩散的图表,它使用与您的应用程序相同的结构和不同的方法。 The main difference is I deploy it in a VPS and I use external tool to launch it so in my package.json file there is not the script command to launch it in prod.主要区别是我将它部署在 VPS 中并使用外部工具启动它,因此在我的package.json文件中没有在 prod 中启动它的script命令。 It is something like:它是这样的:

cd client && npm install && npm run build && cd .. && npm install && node server.js

You can take a look to github repos to get ideas, but I'm going to explain the main differences.您可以查看github 存储库以获取想法,但我将解释主要区别。

  1. server stuff (with package.json ) is in root directory (simply this could solve your problem)服务器的东西(与package.json )在根目录中(这可以解决你的问题)
  2. as per other answers, you need to add two lines to your express app to serve the built client as static files:根据其他答案,您需要在快速应用程序中添加两行,以将构建的客户端作为 static 文件提供服务:
  3. I suggest to use proxy features rather than cors package.我建议使用proxy功能而不是cors package。

In your express app:在您的快递应用程序中:

// At the begenning, with other middlewares
app.use(express.static(path.join(__dirname, "client", "build")));
// As last, to substitute any 404 with your client home page
app.get("*", (req, res) => res.sendFile(path.join(__dirname, "client", "build", "index.html")));

I don't know which kind of data you are going to treat, but CORS is a sort of protection you should never disable (by cors package), at least as it is possible.我不知道您要处理哪种数据,但 CORS 是一种您永远不应该禁用的保护(通过cors包),至少尽可能。 More than this, once you'll be able to solve the reported problem, I'm afraid that following part from the tutorial you used will not work, as it will try to fetch APIs from your app users's localhost:9000 .不仅如此,一旦您能够解决报告的问题,恐怕您使用的教程中的以下部分将无法正常工作,因为它将尝试从您的应用程序用户的localhost:9000获取 API。

callAPI() {
    fetch("http://localhost:9000/testAPI")
        .then(res => res.text())
        .then(res => this.setState({ apiResponse: res }));
}

To both enable CORS and solve fetching from localhost problem you should use proxy feature.要同时启用 CORS 并解决从本地主机获取问题,您应该使用proxy功能。 In your client package.json file just add:在您的客户端package.json文件中添加:

"proxy": "http://localhost:9000/",

This makes your webpack dev server to proxy calls it can't handle to your dev server, than changing your client fetching function to:这会使您的 webpack 开发服务器代理它无法处理的调用到您的开发服务器,而不是将您的客户端获取 function 更改为:

callAPI() {
    fetch("/testAPI")
        .then(res => res.text())
        .then(res => this.setState({ apiResponse: res }));
}

it will automagically works both in dev and prod envs and let you to enable back CORS.它会自动在 dev 和 prod 环境中工作,并让您启用 CORS。

Hope this helps.希望这可以帮助。

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

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