【问题标题】:How to deploy Node.js express server on GCP whithout `gcloud app deploy`?如何在没有`gcloud app deploy`的情况下在GCP上部署Node.js express服务器?
【发布时间】:2020-07-04 01:57:04
【问题描述】:
我不想使用命令 gcloud app deploy 来部署我的 Node.js Express 服务器。
理想情况下,我想:
- 在 VM 实例上克隆存储库
- 运行
npm install
- 运行
npm start,它将在5000端口启动节点服务器。
此类配置的防火墙规则是什么?我会使用虚拟机的外部 IP 将请求发送到我的服务器或其他东西吗? NGINX 在这里扮演什么角色(如果有的话)?
【问题讨论】:
标签:
node.js
express
google-compute-engine
gcloud
mern
【解决方案1】:
您可以在 Google 云上配置常规的 http 和 https 端口,并使用 nginx 作为代理将数据路由到您的应用程序。
Nginx 配置示例:
server {
listen 80;
location / {
proxy_pass http://yourAppAddress:5000/;
}
}
虽然我建议使用 Docker 来部署您的应用程序。
【解决方案2】:
要在 GCP 实例上安装和运行 Node.js express sercer,请按照以下步骤操作(在 Debian9 VM 上测试):
sudo apt update
sudo su -
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt install -y nodejs
curl -L https://npmjs.org/install.sh | sudo sh
npm install -g express-generator
logout
express myproj1
cd myproj1
npm install
npm start
之后你应该会看到
> m1@0.0.0 start /home/wbogacz/m1
> node ./bin/www
关于防火墙 - 添加规则以允许端口 3000 上的 TCP 流量到这台机器;请参阅下面的示例;
gcloud compute --project=myproject firewall-rules create express_rule --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:3000 --source-ranges=0.0.0.0/0 --target-tags=myvm
这假定您的实例具有标签myvm。
之后,您应该能够转到 VM 的外部 IP 并在浏览器页面中看到带有 Welcome to Express 消息的消息。