-
Notifications
You must be signed in to change notification settings - Fork 0
Config Pattern
山间野人 edited this page Feb 26, 2026
·
1 revision
All tunable values externalized:
{
"app": { "name": "Project", "port": 3000 },
"gameplay": { "dropRate": 0.08, "maxLevel": 50 },
"admin": { "password": "secret" }
}var config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
// Public endpoint (strip secrets)
app.get('/api/config', function(req, res) {
var safe = Object.assign({}, config);
delete safe.admin;
res.json(safe);
});
// Admin hot-reload
app.post('/admin/config', requireAdmin, function(req, res) {
fs.writeFileSync('./config.json.bak', fs.readFileSync('./config.json'));
fs.writeFileSync('./config.json', JSON.stringify(req.body, null, 2));
Object.assign(config, req.body);
res.json({ ok: true });
});var APP_CONFIG = null;
fetch('api/config')
.then(function(r) { return r.json(); })
.then(function(data) { APP_CONFIG = data; });All code reads APP_CONFIG.xxx instead of hardcoded values.