我创建了一个快速服务器,将用户添加到 mongoDB。在本地主机上它工作正常。但是,当我将其部署到谷歌云时,它没有向数据库添加任何内容。所以我相信我需要以不同的方式配置我的
connection.js
。这是我正在使用的代码:
连接.js:
const mongoose = require('mongoose');
require('dotenv').config({ path: __dirname + '/../../../.env' });
const dbURI = 'mongodb+srv://' + process.env.DBUSER + ':' + process.env.DBPASSWD + process.env.CLUSTER + '.mongodb.net/' +
process.env.DB + '?retryWrites=true&w=majority';
const dbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
}
const connectToDatabase = async () => {
mongoose.connect(dbURI, dbOptions)
.then(result => console.log("Database connected"))
.catch(error => console.log(error));
};
module.exports = connectToDatabase;
index.js:
const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const handlebars = require("handlebars");
const connectToDatabase = require('./Connection/dbConnection.js');
require('dotenv').config({ path: __dirname + '/./../../.env' })
connectToDatabase();
const app = express();
// Middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));
app.use('', require('./routes/routes.js'));
app.engine('handlebars', exphbs.engine({
defaultLayout: 'main'
}));
app.set("view engine", "handlebars");
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`App listening port ${PORT}`));
这是我为使部署的应用程序正常工作而尝试做的事情:
env_variables.yaml
,并在其中添加了所需的连接变量,如下所示:env_variables:
KEY = 'random-String-here'
DBUSER = 'mondodb-atlas-username'
DBPASSWD = 'mondodb-atlas-password'
DB = 'database name'
CLUSTER = '@cluster123.example'
app.yaml
我有 :includes:
- env_variables.yaml
runtime: nodejs20
connection.js
修改为:const mongoose = require('mongoose');
const fs = require('fs');
// Check if the app is deployed by looking for a deployment-specific environment variable
const isDeployed = process.env.DEPLOYED === 'true';
// Function to load environment variables
const loadEnvVariables = () => {
if (isDeployed) {
// Use env_variables.yaml when deployed
const yaml = require('js-yaml');
const envVarsYaml = fs.readFileSync('env_variables.yaml', 'utf8');
const envVars = yaml.safeLoad(envVarsYaml);
return envVars;
} else {
// Use .env when running locally
require('dotenv').config({ path: __dirname + '/../../../.env' });
return process.env;
}
};
const env = loadEnvVariables();
const dbURI = 'mongodb+srv://' + env.DBUSER + ':' + env.DBPASSWD + env.CLUSTER + '.mongodb.net/' +
env.DB + '?retryWrites=true&w=majority';
const dbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
};
const connectToDatabase = async () => {
mongoose.connect(dbURI, dbOptions)
.then(result => console.log("Database connected"))
.catch(error => console.log(error));
};
module.exports = connectToDatabase;
当然这个解决方案不起作用。每次我尝试添加用户时,我都会得到
Server side error creating a user
即使我明确使用 dbURI
中的环境变量值。所以我想知道我需要采取什么方法,以便如果我正在开发中,代码使用 dotevn
文件,如果部署应用程序,则使用 env_variable
文件连接到数据库。但也许不需要生产和开发的验证,因为mongoDB Atlas
是在线托管的?
第一次部署应用程序并第一次使用
Google Cloud
。任何帮助将不胜感激!
通过将 App Engine 的出站 IP 地址以及 OP 的其他 IP 添加到 MongoDB Atlas 的 IP 访问列表中解决了该问题。这是 MongoDB Atlas 的一项安全功能,仅允许来自项目 IP 访问列表中的条目的连接。
参考资料: