节点应用程序无法在 digitalocean 服务器上运行

node app is not working on digitalocean server

我已经将一个应用程序克隆到我的 Droplet,但现在它无法正常工作,也没有显示任何错误我是编程新手,任何人都可以帮助我部署它。 我已经在其上创建了 droplet install node js、npm 和 mongodb,并且一切正常, 我的 app.js 代码是 让端口 = 80;

      mongoose.connect(process.env.dbURI, dbOptions)
     .then((conn) => {
       app.listen(port,'165.22.223.8');
      console.log('connection success port:' +port);
  })
   .catch((err) => console.log(err));

我怀疑 (!?) 165.22.223.8 不是您的 DigitalOcean Droplet 的 IP 地址。如果这是 Droplet 的正确 public IP 地址,则可能是您没有将 Droplet 的防火墙配置为允许端口 80 上的流量。请参阅下面的调试。

NOTE Ports <1024 (i.e. 80) require root. It's possible that this is another issue you're facing. You may wish to try (the commonly used) 8080 instead of 80 while you're developing. If you do, replace occurrences of 80 with 8080 in what follows.

命令 app.listen 尝试将 Express 服务器绑定到 port 上的特定 IP 地址。

我认为您可以完全避免指定主机名,只需使用:

app.listen(port);

要调试(任一场景),ssh 到 Droplet 并尝试 curling 本地主机的端口 80(假设这确实是 port 的值)即:

HOST="localhost"
PORT="80" # Or 8080 

curl \
--request GET \
http://${HOST}:${PORT}

成功后,您就可以尝试远程访问服务器了。您需要确定 Droplet 的 public IP 地址,然后:

HOST=[[REPLACE-WITH-DROPLET-IP]]
PORT="80" # Or 8080

curl \
--request GET \
http://${HOST}:${PORT}