首先,我从未使用 Heroku 进行托管,因此我的解决方案可能会因您的环境而异
但是,我认为整个概念对你有用。
您的问题没有固定答案,但我现在的想法可能对您有所帮助:)
你的问题有很多子问题,所以我把它分开了。
而且我的英语不太流利,所以你和我之间可能会出现一些误解,
如果我对您的问题的理解有误,请告诉我。
问。但是这种方式可以渲染 React 应用吗?
(我的理解是:react 应用可以由 SSR 托管)
A.您可以使用代码拆分和服务器端渲染为带有 SSR 的 React 应用程序提供服务,您最好使用服务器端渲染
对于某些元标记和 SEO 所需的页面,除非您需要完整的 SSR 应用程序。
如果您需要完整的 ssr,也有适合它的精美框架 Next.js。
通过您提到 Vercel,您可能已经知道这一判断。
问。如果我将 React 应用程序托管在另一台服务器上,可能是 Vercel 或 NGINX 或任何其他服务器,并使用 REST API 访问 heroku 中的后端服务会怎样
(我的理解是:React 应用程序可以托管在另一台服务器上吗?
NGINX = 使用服务器实例,
Vercel:使用无服务器架构
)
A.它也可以像这样托管。似乎这个更像反应。
但我不推荐 Vercel 托管,因为电子商务服务需要很多端点,
查看 Vercel 的定价政策。
问。那么,假设我将 Heroku 用于 Express 后端,是否意味着我需要分别创建 2 个应用程序而不是将它们组合在一起
A.您可以为计算机资源托管多个主机,也可以不托管。如果您计划多主机,您可能需要代理服务器来指向其他服务器,
相反,对于单主机,反向代理指向每个应用程序。
问。 express-subdomain-handler vs. vhost
A.没有固定的答案。你可以使用 vhost 或 express-subdomain-handler 来实现你想要的。或两者兼有。
我的解决方案概念是使用带有反向代理的单个服务器实例。
让 Nginx 在单个服务器中虚拟托管您的多个应用程序。
我的建议:
应用列表。
- react 主机 - nodejs 应用程序
- 客户端 api 主机 - nodejs 应用程序
- 管理员反应主机 - nodejs 应用程序
- 客户端 api 主机 - nodejs 应用程序
我使用 AWS S3 来托管 React 应用程序和
用于 API 应用的 EC2。
结构概念。
( NS / DNS ) : domain.com points your-ip
|
( Server instance )
|
( Nginx ) - virtual hosting
|
( www.domain.com ) --- > client react host : ( node app for server side rendering ) : React rendered file for each route.
Or you can just respond with index.html for all route for skipping SSR
|
( api.domain.com ) --- > clinet api host : api.domain.com/what, api.domain.com/what/ever?you=want
|
( admin.domain.com ) ---> admin react host : admin.domain.com/* : sending index.html //static hosting and let all route point for index.html
|
( admin-api.domain.com ) ---> admin react host : Only if you want to seperate. Or you can combine this with api.domain.com using subrouter
如何构建结构。
第 1 步。服务器托管。
I don't know much about Heroku but I guess there are aws ec2-like service(cloud computing) and s3-like service(static file service),
This can be single or multiple depending your choice. let's say you wanna go with single server and use virtual host for multiple service end point.
Step 2. 网络层设置
let's say name-server and DNS server part is network layer. I used AWS Route 53 for this.
2.1 Buy an domain
www.your-service.com
2.2 Add some CNAME to point your web-server host
- your-service.com,
- www.your-service.com
- api.your-service.com,
- admin.your-service.com
.
.
.
Step 3.创建nodejs应用(应用层)
I'm assuming that you had one already judging by code in your question.
ubuntu@ip-your-ip:~/project$ ls
api cli admin admin-api config libraries middlewares models node_modules package.json
ubuntu@ip-your-ip:~/project$ cd api
ubuntu@ip-your-ip:~/project/api$ ls
README.md app.js bin config.json pm2.config.js node_modules package.json routes
ubuntu@ip-your-ip:~/project/api$ pm2 start pm2.config
PM2 是节点应用程序 deamonizer,您可以为此使用任何守护程序。
PM2 参考。
https://pm2.keymetrics.io/docs/usage/application-declaration/
步骤 3. nodejs 应用程序的 Nginx 点
nano /etc/nginx/nginx.conf
listen 80;
server_name client.domain.com;
location / {
proxy_http_version 1.1;
proxy_pass http://localhost:3000;
}
listen 80;
server_name api.domain.com;
location / {
proxy_http_version 1.1;
proxy_pass http://localhost:3001;
}
listen 80;
server_name admin.domain.com;
location / {
proxy_http_version 1.1;
proxy_pass http://localhost:4000;
}
listen 80;
server_name admin-api.domain.com;
location / {
proxy_http_version 1.1;
proxy_pass http://localhost:4001;
}
我希望这个解决方案对你有帮助:)