|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var express = require('../../'); |
| 8 | + |
| 9 | +// Load environment variables from .env file |
| 10 | +// This should be done BEFORE creating the app |
| 11 | +// Automatically loads .env, .env.[NODE_ENV], and .env.local |
| 12 | + |
| 13 | +// Option 1: Simple load (recommended for production) |
| 14 | +express.loadEnv(); |
| 15 | + |
| 16 | +// Option 2: Load with file watching (useful for development) |
| 17 | +// Uncomment below to enable automatic reloading when .env files change |
| 18 | +/* |
| 19 | +var unwatch = express.loadEnv({ |
| 20 | + watch: true, |
| 21 | + onChange: function(changed, loaded) { |
| 22 | + console.log('Environment variables changed:'); |
| 23 | + Object.keys(changed).forEach(function(key) { |
| 24 | + var change = changed[key]; |
| 25 | + if (change.type === 'added') { |
| 26 | + console.log(' + ' + key + ' = ' + change.value); |
| 27 | + } else if (change.type === 'modified') { |
| 28 | + console.log(' ~ ' + key + ': ' + change.oldValue + ' -> ' + change.newValue); |
| 29 | + } else if (change.type === 'removed') { |
| 30 | + console.log(' - ' + key + ' (was: ' + change.oldValue + ')'); |
| 31 | + } |
| 32 | + }); |
| 33 | + }, |
| 34 | + onError: function(err) { |
| 35 | + console.error('Error reloading environment variables:', err.message); |
| 36 | + } |
| 37 | +}); |
| 38 | +
|
| 39 | +// To stop watching (e.g., on app shutdown): |
| 40 | +// unwatch(); |
| 41 | +*/ |
| 42 | + |
| 43 | +var app = module.exports = express(); |
| 44 | +var logger = require('morgan'); |
| 45 | + |
| 46 | +// custom log format |
| 47 | +if (process.env.NODE_ENV !== 'test') app.use(logger(':method :url')) |
| 48 | + |
| 49 | +app.get('/', function(req, res){ |
| 50 | + var nodeEnv = process.env.NODE_ENV || 'development'; |
| 51 | + res.send('<h1>Environment Variables Example</h1>' |
| 52 | + + '<p>This example demonstrates how to use Express built-in .env file loading.</p>' |
| 53 | + + '<p><strong>Current Environment:</strong> ' + nodeEnv + '</p>' |
| 54 | + + '<ul>' |
| 55 | + + '<li><a href="/env">View loaded environment variables</a></li>' |
| 56 | + + '<li><a href="/config">View application configuration</a></li>' |
| 57 | + + '</ul>' |
| 58 | + + '<hr>' |
| 59 | + + '<p><strong>Features:</strong></p>' |
| 60 | + + '<ul>' |
| 61 | + + '<li>Automatically loads .env, .env.[NODE_ENV], and .env.local files</li>' |
| 62 | + + '<li>Optional file watching for automatic reloads (see source code)</li>' |
| 63 | + + '<li>Environment-specific configuration</li>' |
| 64 | + + '<li>Cascading values with .env.local overrides</li>' |
| 65 | + + '</ul>' |
| 66 | + + '<hr>' |
| 67 | + + '<p><strong>Try running with different environments:</strong></p>' |
| 68 | + + '<pre>NODE_ENV=development node index.js\nNODE_ENV=production node index.js</pre>' |
| 69 | + + '<p><strong>Enable watch mode:</strong> Uncomment the watch example in index.js and modify your .env file to see live updates!</p>'); |
| 70 | +}); |
| 71 | + |
| 72 | +app.get('/env', function(req, res){ |
| 73 | + // Display some environment variables |
| 74 | + var safeEnvVars = { |
| 75 | + APP_NAME: process.env.APP_NAME, |
| 76 | + APP_ENV: process.env.APP_ENV, |
| 77 | + PORT: process.env.PORT, |
| 78 | + DEBUG: process.env.DEBUG, |
| 79 | + API_URL: process.env.API_URL, |
| 80 | + MAX_ITEMS: process.env.MAX_ITEMS |
| 81 | + }; |
| 82 | + |
| 83 | + res.send('<h1>Environment Variables</h1>' |
| 84 | + + '<p>The following variables were loaded from .env file:</p>' |
| 85 | + + '<pre>' + JSON.stringify(safeEnvVars, null, 2) + '</pre>' |
| 86 | + + '<p><a href="/">Back to home</a></p>'); |
| 87 | +}); |
| 88 | + |
| 89 | +app.get('/config', function(req, res){ |
| 90 | + // Use environment variables for configuration |
| 91 | + var config = { |
| 92 | + appName: process.env.APP_NAME, |
| 93 | + environment: process.env.APP_ENV, |
| 94 | + port: process.env.PORT || 3000, |
| 95 | + debug: process.env.DEBUG === 'true', |
| 96 | + apiUrl: process.env.API_URL, |
| 97 | + maxItems: parseInt(process.env.MAX_ITEMS) || 10 |
| 98 | + }; |
| 99 | + |
| 100 | + res.send('<h1>Application Configuration</h1>' |
| 101 | + + '<p>Configuration built from environment variables:</p>' |
| 102 | + + '<pre>' + JSON.stringify(config, null, 2) + '</pre>' |
| 103 | + + '<p><a href="/">Back to home</a></p>'); |
| 104 | +}); |
| 105 | + |
| 106 | +/* istanbul ignore next */ |
| 107 | +if (!module.parent) { |
| 108 | + var port = process.env.PORT || 3000; |
| 109 | + app.listen(port); |
| 110 | + console.log('Express started on port ' + port); |
| 111 | + console.log('App Name: ' + (process.env.APP_NAME || 'Not set')); |
| 112 | + console.log('Environment: ' + (process.env.APP_ENV || 'Not set')); |
| 113 | +} |
0 commit comments