Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:Environment variables are used in the LostData project to manage external configuration, primarily for the server port and the database connection string. This allows the application to be deployed in different environments (e.g., development, production) without changing the source code.
The project utilizes the dotenv package to load these variables from a .env file into process.env at runtime. This is a standard practice in Node.js applications for handling environment-specific settings securely and flexibly.
Setup and Loading
The application relies on the dotenv package to manage environment variables. This is declared as a dependency in the package.json file.
Sources: package.json:5
To ensure environment variables are available throughout the application, the dotenv package is initialized at the very top of the main server file, server.js. The line require("dotenv").config(); loads the variables from a .env file in the project's root directory.
// server.js:1
require("dotenv").config();
Sources: server.js:1
The following diagram illustrates the application's startup sequence, highlighting when environment variables are loaded.
This diagram shows that loading environment variables is the first step in the application's execution flow.
Sources: server.js:1-2
Usage and Configuration
Two primary environment variables are used in this project: PORT for the web server and MONGODB_URI for the database connection.
Environment Variables Table
| Variable | Description | Default Value | Used In |
|---|---|---|---|
MONGODB_URI | The connection string for the MongoDB database. | "mongodb://127.0.0.1:27017/model_db" | APP/config/db.config.js, server.js, seed.js |
PORT | The port on which the Express server will listen for requests. | 3000 | server.js |
Sources: APP/config/db.config.js:2, server.js:25, 126
Database Connection (MONGODB_URI)
The MongoDB connection URI is managed in APP/config/db.config.js. This file exports a configuration object that provides the value of process.env.MONGODB_URI if it exists, otherwise, it falls back to a local MongoDB instance.
// APP/config/db.config.js:1-3
module.exports = {
url: process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/model_db"
};
Sources: APP/config/db.config.js:1-3
This configuration is then imported by APP/models/index.js, which centralizes database-related objects, and subsequently used in server.js to establish the connection.
// server.js:24-25
const db = require("./APP/models");
db.mongoose.connect(process.env.MONGODB_URI).then(() => console.log("DB connected")).catch(err => console.log(err));
Sources: server.js:24-25, APP/models/index.js:3,7
The seed.js script also uses this configuration to connect to the database and populate it with initial data.
// seed.js:123-124
db.mongoose.connect(db.url)
.then(() => {
Sources: seed.js:123-124, APP/models/index.js:7
The following sequence diagram shows how the MONGODB_URI is resolved and used to connect to the database when the server starts.
This diagram illustrates the flow of retrieving the database URL from the configuration files and using it to establish a connection.
Sources: server.js:24-25, APP/models/index.js:3,7, APP/config/db.config.js:2
Server Port (PORT)
The PORT environment variable determines which port the Express server listens on. If process.env.PORT is not set, the application defaults to port 3000. This is defined at the end of server.js.
// server.js:125-126
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server on ${PORT}`));
Sources: server.js:125-126
Conclusion
The use of environment variables for PORT and MONGODB_URI is a key architectural feature of the LostData application. It decouples the application's runtime configuration from the source code, enabling seamless deployment across different environments and improving the maintainability and security of the system. The dotenv package simplifies the management of these variables during local development.