简体   繁体   English

如何在 Node.js 应用程序中最好地保护 MongoDB 数据库 url

[英]How to best secure MongoDB database url in Node.js app

I have an API built on MongoDB and Node.js.我有一个基于 MongoDB 和 Node.js 的 API。 In my app.js I have a database url that I need to include, with a password in the url.在我的app.js ,我需要包含一个数据库网址,网址中包含密码。 Is there a secure way to define this url as a variable in another file for when it is deployed?是否有一种安全的方法可以在部署时将此 url 定义为另一个文件中的变量? I will likely be using Heroku to deploy this.我可能会使用 Heroku 来部署它。

app.js应用程序.js

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

const product = require("./routes/product.route"); // Imports routes for the products
const app = express();

// Set up mongoose connection
const mongoose = require("mongoose");
// Change db url for new apps
let dev_db_url = "URL FOR MONGODB DATABASE TO BE SECURE";
const mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false });
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));

由于您使用的是 Heroku,因此存储凭据的最佳方法是将它们定义为配置变量,然后可以从 Node.js 中的process.env访问它们。

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

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