[英]How can I deploy a GraphQL API and its React Client on Heroku?
I have created an application with a GraphQL API and a React Client.我用 GraphQL API 和 React 客户端创建了一个应用程序。 My project's structure is as follows:
我的项目结构如下:
project
│ README.md
│
└───server
│ │ package.json
│ │
│ └───src
│ │ index.ts
│ │
│ └───entity/migration/resolver/etc.
│ │ ...
│
└───client
│ │ package.json
│ │
│ └───public
│ │ │ index.html
| | | ...
│ │ │
│ └───src
│ │ index.js
│ │
│ └───components
│ │ ...
I'd like to deploy this entire app to Heroku, but I'm not quite sure how to do so.我想将整个应用程序部署到 Heroku,但我不太确定该怎么做。 On my local machine, I have my server running on
localhost:4000
and my client running on localhost:3000
, and my client makes requests to my server using @apollo/client
.在我的本地机器上,我的服务器在
localhost:4000
上运行,我的客户端在localhost:3000
上运行,我的客户端使用@apollo/client
向我的服务器发出请求。 Would I need to deploy two separate apps to Heroku, or is there a way to deploy these both as one app?我需要将两个单独的应用程序部署到 Heroku,还是有办法将这两个应用程序部署为一个应用程序?
Here's the code from my server/src/index.ts
:这是来自我的
server/src/index.ts
的代码:
import dotenv from "dotenv"
import "reflect-metadata"
import { ApolloServer } from "apollo-server"
import { buildSchema } from "type-graphql"
import { createConnection } from "typeorm"
(async () => {
await createConnection()
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [`${__dirname}/resolver/*.ts`],
})
})
const port = process.env.PORT || 4000
apolloServer.listen(port, () => {
console.log(`Server started on port: ${port}`)
})
})()
And here's the code from my client/src/index.js
:这是来自我的
client/src/index.js
的代码:
import React from "react"
import ReactDOM from "react-dom"
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client"
import App from "./components/App"
const client = new ApolloClient({
uri: process.env.ENDPOINT || "http://localhost:4000",
cache: new InMemoryCache()
})
ReactDOM.render(<ApolloProvider client={client}><App /></ApolloProvider>, document.getElementById("root"))
It's a long topic to be covered that's exactly what I have started developing recently, the entire source code is available on Github at https://github.com/mdarif/project-management这是一个很长的话题,这正是我最近开始开发的内容,整个源代码可在 https 的 Github 上获得://github.com/mdarif/project-management
This is MERN app where I have deployed Server/Client and MongoDB all in once on Heroku, there is also a Dockerize Version of the same MERN app available using Github Actions.这是 MERN 应用程序,我在 Heroku 上一次性部署了服务器/客户端和 MongoDB,还有一个使用 Github 操作的相同 MERN 应用程序的 Dockerize 版本。
Below important things should be taken care of:应注意以下重要事项:
server/index.js or server.js
server/index.js or server.js
中的客户端构建// Serve Frontend
if (process.env.NODE_ENV === 'production') {
// Set build folder as static folder
app.use(express.static(path.join(__dirname, '../client/build')))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../client/build/index.html'))
})
} else {
app.get('/', (req, res) => {
res.status(200).json({ message: 'Welcome to the Project...' })
})
}
"scripts": {
...
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
And that's it.就是这样。 You're done.
你完成了。 Your app would now be deployed, If you face errors.
如果您遇到错误,您的应用程序现在将被部署。 you can debug them using Heroku's logs.
你可以使用 Heroku 的日志来调试它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.