简体   繁体   English

使用客户端和服务器在两个单独的文件夹中将节点应用程序部署到 heroku

[英]Deploying node app to heroku with client and server in two separate folders

I've done a bunch of research and I can't quite seem to wrap my head around this.我做了很多研究,但我似乎无法完全理解这一点。

I've built an app.我已经构建了一个应用程序。 The client was built with Vue-cli and runs on port 8080 from a client folder, and the server from a separate server folder on port 8081 .客户端是使用 Vue-cli 构建的,并从客户端文件夹在port 8080上运行,服务器从port 8081上的单独服务器文件夹运行。 In essence, I have this:本质上,我有这个:

client
    - package.json
    - node_modules
    - src
    - build
    - index.html

server
    - package.json
    - node_modules
    - app.js
    - auth.js

I'm unsure of how to resolve the folder structure so that I can deploy this to Heroku.我不确定如何解析文件夹结构以便将其部署到 Heroku。

According to a bunch of research I've done and this answer (admittedly quite an old post), one suggestion is to combine the two, but how would I resolve the two package.json files that I have in each folder (client and server)?根据我所做的一系列研究和这个答案(诚然是一篇很旧的帖子),一个建议是将两者结合起来,但我将如何解决每个文件夹(客户端和服务器)中的两个 package.json 文件)? Do I merge them?我合并它们吗?

Another suggestion is to create two separate Heroku apps.另一个建议是创建两个独立的 Heroku 应用程序。 I can then set my axios baseURL to app_name.herokuapp.com ?然后我可以将我的axios baseURL 设置为app_name.herokuapp.com吗?

Which of the two is generally considered the ideal solution?两者中哪一个通常被认为是理想的解决方案? I'm really stuck here...我真的被困在这里...

Well, I came across the same problem.好吧,我遇到了同样的问题。 We can give Heroku a script to run after it install dependencies heroku-postbuild .我们可以给 Heroku 一个脚本,让它在安装依赖heroku-postbuild后运行。

My Repository Folder Structure我的存储库文件夹结构

App
|
+-- client               (folder)  (root)
|   |
|   +--src               (folder)
|   +--public            (folder)
|   +--package.json      (file)
|   +--package-lock.json (file)
|
+-- server               (folder)  (root)
|   |
|   +--....              (folders)
|   +--....              (folders)
|   +--index.js          (Server Main File)
|   +--package.json      (file)
|   +--package-lock.json (file)
|
+-- package.json         (Dummy package JSON for detection of heroku)

My Git Hub Repo我的 Git Hub 回购

I have added the package.json to Root folder with the following code.我已使用以下代码将package.json添加到 Root 文件夹。

{
    "name": "rollcall-rooms",
    "version": "1.0.0",
    "main": "",
    "scripts": {
        "start": "npm start --prefix server",
        "install-client": "cd client && npm install && npm run build && cd ..",
        "install-server": "cd server && npm install && cd .. ",
        "heroku-postbuild": "npm run install-client && npm run install-server"
    }
}

Deployment Process In Heroku Heroku 中的部署过程

  • Clones your repo.克隆你的回购协议。
  • Detects your package.json in the root folder and install all the node dependencies.在根文件夹中检测您的package.json并安装所有节点依赖项。
  • Now it will run the script heroku-postbuild .现在它将运行脚本heroku-postbuild TUDUM .图杜姆 You can give your own magic here.你可以在这里施展你自己的魔法。

As far as I know nowadays making two deploys is the most used solution for big projects evolving many devs, in one part your frontend with vue.js that will fetch the data from a remote api which is your backend and second deploy.据我所知,现在进行两次部署是开发许多开发人员的大型项目最常用的解决方案,一部分是你的前端使用 vue.js,它将从远程 api 获取数据,这是你的后端和第二次部署。 Exactly, you will have to change your baseURL to app_name.herokuapp.com Also you will probably will have to enable CORS.确切地说,您必须将 baseURL 更改为app_name.herokuapp.com此外,您可能还必须启用 CORS。

Also if you want to try new things I recommend you try surge for your front deployment;) so easy and so fast!另外,如果您想尝试新事物,我建议您尝试在前端部署中使用surge ;)如此简单、如此快速!

Maybe you already know it but, Heroku sets it's own ports so you will need to create a .env file and assign it via ssh or manually in the Heroku dashboard.也许您已经知道了,但是 Heroku 设置了自己的端口,因此您需要创建一个.env文件并通过 ssh 或在 Heroku 仪表板中手动分配它。

This repo shows the setup of Node.js serving up a React frontend running on a single Heroku dyno: https://github.com/mars/heroku-cra-node这个 repo 展示了 Node.js 的设置,服务于在单个 Heroku dyno 上运行的 React 前端: https ://github.com/mars/heroku-cra-node

I was able to get one up and running using this as a guide.我能够以此为指导启动并运行。 For cleanliness i modified my folder structure to be very similar to op's: client/ server/ package.json.gitignore.env (etc)为了清洁起见,我将文件夹结构修改为与 op 非常相似: client/ server/ package.json.gitignore.env (etc)

I think Mark Brought up a good repo, but I wanted to emphasize how Heroku works.我认为 Mark 提出了一个很好的回购协议,但我想强调 Heroku 的工作原理。 As Heroku documentation said "Heroku Node.js support will only be applied when the application has a package.json file in the root directory."正如 Heroku 文档所说"Heroku Node.js support will only be applied when the application has a package.json file in the root directory."

What should I do then?那我该怎么办?

When deploying both a client and server code together, you would want to put the server's code at the root of the folder and continue to keep the client's code in a separate folder.当同时部署客户端和服务器代码时,您可能希望将服务器代码放在文件夹的根目录下,并继续将客户端代码保存在单独的文件夹中。

Example of a React and Express Application folder structure : React 和 Express 应用程序文件夹结构示例

App
|
+-- client               (folder)  (root)
|   |
|   +--node_modules      (folder)
|   +--src               (folder)
|   +--public            (folder)
|   +--package.json      (file)
|   +--package-lock.json (file)
|         
+-- node_modules         (folder)  (root)       
+-- index.js             (file)    (root)
+-- package.json         (file)    (root)
+-- package-lock.json    (file)    (root)

As you can see, the server code is all in the root folder while the react application is kept in the client folder.如您所见,服务器代码都在根文件夹中,而 React 应用程序保存在客户端文件夹中。 From here, you need to make sure that you set up the correct script for your server package.json.从这里开始,您需要确保为您的服务器 package.json 设置正确的脚本。

Remember that the server's package.json is instruction for Heroku on how to start your application.请记住,服务器的 package.json 是 Heroku 关于如何启动您的应用程序的说明。

This is what a proper script would look like in the root server's package.json这是根服务器的 package.json 中正确脚本的样子

"scripts": {
    "start": "node index.js",
    "heroku-postbuild": "cd client && npm install && npm run build"
  } 

I had the same problem.我有同样的问题。 I had my app structure divided into an "api" repo built with express-generator and a "client" repo built with create-react-app.我将我的应用程序结构分为使用 express-generator 构建的“api”存储库和使用 create-react-app 构建的“客户端”存储库。 I ended up having to scrap my whole api repo and rewrite it as a single file in the root directory named index.js that houses all the express code and has its own package.json.我最终不得不废弃我的整个 api 存储库并将其重写为根目录中名为 index.js 的单个文件,该目录包含所有 express 代码并拥有自己的 package.json。 I followed this tutorial: https://medium.com/@chloechong.us/how-to-deploy-a-create-react-app-with-an-express-backend-to-heroku-32decfee6d18 .我遵循了本教程: https ://medium.com/@chloechong.us/how-to-deploy-a-create-react-app-with-an-express-backend-to-heroku-32decfee6d18。 I was then able to host it all on heroku in one app.然后我就可以在一个应用程序中将其全部托管在 heroku 上。 you can keep them seperate but that seemed too complicated for me and then you would have to host them in two different places and possibly need Cross origin resource sharing.您可以将它们分开,但这对我来说太复杂了,然后您必须将它们托管在两个不同的地方,并且可能需要跨源资源共享。 My New file structure looks alot like the example in Henry Lis' post我的新文件结构看起来很像 Henry Lis 帖子中的示例

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

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