问题描述
我正在尝试将我的MERN应用程序部署到数字海洋小滴(Ubuntu 20.04服务器)上.
I am trying to deploy my MERN app to a digital ocean droplet (Ubuntu 20.04 server).
我将GitHub存储库克隆到了服务器.
I cloned my GitHub repo to the server.
现在,当我尝试使用 npm start
启动服务器时,出现以下错误.代码段如下:
Now, when I am trying to start the server using npm start
, I get the following error.The code snippet is as follows:
服务器/配置/db.js
const mongoose = require("mongoose");
const colors = require("colors");
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
});
console.log(`MongoDB connected: ${conn.connection.host}`.cyan.bold);
} catch (error) {
console.error(`Error: ${error.message}`.red.bold.underline);
process.exit(1);
}
};
2;
module.exports = connectDB;
但是,在我的本地计算机上一切正常.如果我 console.log(process.env.MONGO_URI)
,我会得到字符串.
However, everything works fine on my local machine. If I console.log(process.env.MONGO_URI)
, I get the string.
在小滴中,我尝试执行以下操作:
In the droplet, I tried doing the following:
导出MONGO_URI = the_connection_string
.即使那样,我仍然会收到错误消息.
export MONGO_URI=the_connection_string
. Even then, I am getting the error.
我在做什么错了?
错误
推荐答案
我发现了问题.我已经将 .env
放在 .gitignore
内.因此 .env
在github存储库中不可用,我已将其克隆到我的Digital Ocean液滴中.作为解决方案,我使用 vim
在我的Droplet中重新创建了 .env
文件.这样我就可以启动服务器了.
I found the issue. I had put .env
inside .gitignore
. Therefore .env
was not available in the github repo, which I had cloned to my Digital Ocean droplet. As a solution, I recreated the .env
file inside my droplet using vim
. Then I could start the server without any issue.
这篇关于无法在数字海洋小滴中启动服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!