101k views
5 votes
besides keeping models, views, and controllers in separate files and scripts, we also want to separate our connection to the database. which file houses the credentials and other confidential values for connection to mongodb, so that it is hidden from the source code?

1 Answer

3 votes

Final answer:

To securely store MongoDB connection details, they are typically housed in a separate .env file or a config.js file, which should not be checked into source control. Developers use environment variables and libraries like dotenv to manage confidential values and keep them hidden from the source code.

Step-by-step explanation:

In the context of web development, particularly when working with a MongoDB database, it's crucial to store connection strings and credentials securely. These sensitive details are typically placed in a separate configuration file or environment variables, often named .env or config.js. This approach not only keeps your credentials out of the source code but also makes it easier to manage different settings across various deployment environments, such as development, testing, and production.

To implement this, developers use environment variables defined in an ignored file (.env) or a configuration module that is not checked into source control. Libraries such as dotenv for Node.js can be used to load these variables into the application at runtime. This helps keep sensitive data like URI strings, passwords, and API keys out of the codebase, thus protecting it from being exposed publicly or accidentally shared. Also, it's a good security practice to define role-based access control in your database management system to further secure the data.

User Jurrian
by
7.6k points