无法在数字海洋小滴中启动服务器

编程入门 行业动态 更新时间:2024-10-15 02:28:58
本文介绍了无法在数字海洋小滴中启动服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将我的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.

更多推荐

无法在数字海洋小滴中启动服务器

本文发布于:2023-10-28 04:04:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:海洋   服务器   数字   小滴中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!