Production Configuration
Production configuration is the set of values the live app runs with. The values come from the server, and secrets stay hidden.
A .env.production file
The live server holds its own copy of the file.
MONGO_URI=mongodb://localhost:27017/notes
PORT=3001
JWT_SECRET=change-this-secretA config loader
One object gathers every setting the app needs.
const config = {
mongoUri: process.env.MONGO_URI,
port: process.env.PORT || 3001
};Example
process.env.MONGO_URI = 'mongodb://localhost:27017/notes';
process.env.PORT = '3001';
process.env.JWT_SECRET = 'super-secret-value';
function mask(value) {
return value ? '***' : 'missing';
}
console.log('MONGO_URI = ' + process.env.MONGO_URI);
console.log('PORT = ' + process.env.PORT);
console.log('JWT_SECRET = ' + mask(process.env.JWT_SECRET));Key points
- Each server holds its own values for the same setting names.
- A config loader reads every value in one place.
- Missing values must fail loudly at startup, not later.
- A secret is printed as stars, never in full.
In this example, the script sets three values first. It then prints each resolved value in turn. The secret passes through a mask, so only stars appear.
Note
The same code runs on every server with different values.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
How should a secret appear in printed output?