简体   繁体   English

数字海洋托管的node.js / express应用显示无法获取/错误

[英]node.js/express app hosted with digital ocean displays the Cannot get / error

After looking online and seeing countless different answers to different situations, I still couldn't find a solution that worked for me. 在网上浏览并看到针对不同情况的无数不同答案之后,我仍然找不到适合我的解决方案。 When I try to run my server.js file locally, it works fine. 当我尝试在本地运行server.js文件时,它可以正常运行。 But when I copy my repository and tried running it in my droplet I get this error: 但是,当我复制存储库并尝试在其Droplet中运行它时,出现此错误:

Cannot GET /

The following is my server.js code: 以下是我的server.js代码:

const express = require('express')
const path = require('path')
const history = require('connect-history-api-fallback')

const app = express()

const staticFileMiddleware = express.static(path.join(__dirname + '/dist'))

app.use(staticFileMiddleware)
app.use(history({
  disableDotRule: true,
  verbose: true
}))
app.use(staticFileMiddleware)

app.get('/', function (req, res) {
  res.render(path.join(__dirname + '/dist/index.html'))
})

var server = app.listen(process.env.PORT || 80, function () {
  var port = server.address().port
  console.log('App now running on port', port);
  console.log(path.join(__dirname + '/dist/index.html'));
})

I recommend starting with an auto-generated express project by doing: 我建议从执行一个自动生成的快递项目开始:

npm install -g express-generator
express --view=jade new-project-folder-name

... if you do that then DEBUG=* node bin/www will launch the project on port 3000 with all debug enabled. ...如果这样做,那么DEBUG=* node bin/www将在启用所有调试的情况下在端口3000上启动项目。

Practically speaking you probably don't actually want to launch your express project on port 80, but rather, you'd want to use a proper web server like Nginx or Apache, and then configure it as a reverse proxy to your express application on a local port (like 3000). 实际上,您实际上可能并不希望在端口80上启动Express项目,而是希望使用适当的Web服务器(如Nginx或Apache),然后将其配置为对您的Express应用程序的反向代理。本地端口(例如3000)。 Also use UFW to reduce the security surface area of your server. 还可以使用UFW减少服务器的安全表面积。

That way you can also use Lets Encrypt to let the web server manage all the SSL business and leave your express application to just worry about its business. 这样,您还可以使用Lets Encrypt来让Web服务器管理所有SSL业务,而让您的快速应用程序只担心其业务。

For a start though, just try launching your project on port 3000 and visiting the site at http://your-ip-address:3000 and see if you get a different result. 首先,请尝试在端口3000上启动您的项目,然后访问http:// your-ip-address:3000上的站点,看看是否得到不同的结果。 If you do, you probably need to use the internet to piece together a solution that looks similar to what I described above. 如果这样做,您可能需要使用互联网来拼凑一个类似于我上面所描述的解决方案的解决方案。 Digital Ocean tutorials are pretty good in this regard. 在这方面,Digital Ocean教程非常出色。

The problem was that when I cloned my repository, it didn't push my dist folder. 问题是,当我克隆存储库时,它没有推送我的dist文件夹。 So the solution to my problem was to create a separate repo and push my dist folder manually. 因此,解决我的问题的方法是创建一个单独的存储库,然后手动推送dist文件夹。 I then used this repo to serve my app on my droplet. 然后,我使用此存储库在我的Droplet上投放了我的应用程序。

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

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