{"id":23819832,"url":"https://github.com/kth/kth-node-configuration","last_synced_at":"2026-03-08T13:35:49.621Z","repository":{"id":73510697,"uuid":"67044510","full_name":"KTH/kth-node-configuration","owner":"KTH","description":"Configuration module for Node.js projects.","archived":false,"fork":false,"pushed_at":"2025-05-01T04:04:31.000Z","size":1209,"stargazers_count":0,"open_issues_count":7,"forks_count":0,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-06-09T00:39:45.561Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KTH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-31T14:24:11.000Z","updated_at":"2025-02-12T13:11:16.000Z","dependencies_parsed_at":"2024-01-26T20:43:17.164Z","dependency_job_id":"1795ad9f-9b51-49aa-888c-9af6886b9403","html_url":"https://github.com/KTH/kth-node-configuration","commit_stats":{"total_commits":125,"total_committers":11,"mean_commits":"11.363636363636363","dds":0.48,"last_synced_commit":"7df76d74826f93dd237c66a90127ccbac1054b16"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"purl":"pkg:github/KTH/kth-node-configuration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KTH%2Fkth-node-configuration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KTH%2Fkth-node-configuration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KTH%2Fkth-node-configuration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KTH%2Fkth-node-configuration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KTH","download_url":"https://codeload.github.com/KTH/kth-node-configuration/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KTH%2Fkth-node-configuration/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263711154,"owners_count":23499829,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-02T07:16:03.681Z","updated_at":"2026-03-08T13:35:49.615Z","avatar_url":"https://github.com/KTH.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kth-node-configuration\n\nConfiguration module for Node.js projects.\n\n## Usage\n\n### node-api projects\n\nIn node-api projects you only have a single settings file called serverSettings.js. Create your configuration by adding the following code:\n\n```javascript\n'use strict'\nconst { generateConfig } = require('kth-node-configuration')\n\n// These settings are used by the server\nconst serverConfig = generateConfig([require('../../../config/serverSettings')])\n\nmodule.exports.server = serverConfig\n```\n\nAll options are available in this object.\n\n### node-web projects\n\nIn node-web projects your settings are split in three files:\n\n- commonSettings.js -- settings shared by browser and server (i.e. don't store any secrets here)\n- browserSettings.js -- settings passed to the browser (i.e. don't store any secrets here either)\n- serverSettings.js -- settings that are specific to the server\n\nIf you use LDAP you will also want to add default LDAP settings to your server config.\n\n```javascript\n'use strict'\nconst { generateConfig } = require('kth-node-configuration')\n// The ldapDefaultSettings contains ldapClient defaults object\nconst ldapDefaultSettings = require('kth-node-configuration').unpackLDAPConfig.defaultSettings\n\n// These settings are used by the server\nconst serverConfig = generateConfig([\n  ldapDefaultSettings,\n  require('../../../config/commonSettings'),\n  require('../../../config/serverSettings')\n])\n\nmodule.exports.server = serverConfig\n\n// These settings are passed to the browser\nconst browserConfig = generateConfig([\n  require('../../../config/commonSettings'),\n  require('../../../config/browserSettings')\n])\n\nmodule.exports.browser = browserConfig\n```\n\n### Helper methods\n\nThere are a couple of helper methods available to allow your settings files to be a bit more concise.\n\nThe env-vars should be on the same form as the default URI.\n\nOptions override any settings you pass through env-vars or defaults.\n\nNOTE: the helper methods obey standard URI syntax. Any get params you add will be set as properties\non the config object.\n\n#### Escaping\n\nDon't forget to escape special characters such as:\n\n- '\u0026' in keys to '%26'\n- '/' in usernames or passwords to '%2F'\n\n#### unpackApiKeysConfig(ENV_VAR_NAME_URI, defaultURI)\n\nThis call returns an array of api access key objects.\n\n```javascript\nconst defaultUri = '?name=devClient\u0026apiKey=1234\u0026scope=write\u0026scope=read'\nunpackApiKeysConfig('API_KEYS', defaultUri)\n```\n\nTo define multiple API_KEYS you name each key as if it was a reference to an array. The unpacker will iterate from 0 and\nadd each item until it comes across a value that is undefined:\n\n```\nAPI_KEYS_0 = '?name=devClient\u0026apiKey=1234\u0026scope=write\u0026scope=read'\nAPI_KEYS_1 = '?name=devClient\u0026apiKey=1234\u0026scope=write\u0026scope=read'\nAPI_KEYS_2 = '?name=devClient\u0026apiKey=1234\u0026scope=write\u0026scope=read'\n```\n\n#### unpackKOPPSConfig(ENV_VAR_NAME_URI, defaultURI [, options])\n\n```javascript\nconst defaultUri = 'http://[hostname][:port][/path]?defaultTimeout=60000'\nunpackKOPPSConfig('KOPPS_URI', defaultUri)\n```\n\n#### unpackLDAPConfig(ENV_VAR_NAME_URI, ENV_VAR_NAME_PASSWORD, defaultURI [, options])\n\n```javascript\n// Never hard code defaults to LDAP in settings, always pass through env-vars\n// LDAP_URI = 'ldaps://[username]@[hostname]:[port]\n// LDAP_PASSWORD = 'yadayada'\nunpackLDAPConfig('LDAP_URI', 'LDAP_PASSWORD')\n```\n\nNOTE 1: Some default settings are always applied and can be overridden by passing options. Check source for defaults.\n\nNOTE 2: Having a separate config.ldap and config.ldapClient configuration is deprecated, everything should be in config.ldap.\n\nNOTE 3: upackRedisConfig supports Azure connection string\n\n#### unpackMongodbConfig(ENV_VAR_NAME_URI, defaultURI [, options])\n\n```javascript\nconst defaultUri = 'mongodb://[hostname][:port][/path][?ssl=true]'\nunpackMongodbConfig('MONGODB_URI', defaultUri)\n```\n\n#### unpackNodeApiConfig(ENV_VAR_NAME_URI, defaultURI [, options])\n\n```javascript\nconst defaultUri = 'http[s]://[hostname][:port][/path][?required=true\u0026defaultTimeout=10000]'\nunpackNodeApiConfig('NODE_API_URI', defaultUri)\n```\n\n#### unpackRedisConfig(ENV_VAR_NAME_URI, defaultURI [, options])\n\n```javascript\nconst defaultUri = 'redis://[hostname][:port]/'\nunpackRedisConfig('REDIS_URI', defaultUri)\n```\n\n#### unpackSMTPConfig(ENV_VAR_NAME_URI, defaultURI [, options])\n\n```javascript\n// Never include username or password in default SMTP-config\nconst defaultUri = 'smtp://smtp.kth.se:25'\n// SMTP_URI = smtp[s]://[username][:password]@[hostname]:[port]\nunpackSMTPConfig('SMTP_URI', defaultUri)\n```\n\n#### unpackSequelizeConfig(ENV_VAR_NAME_URI, defaultURI [, options])\n\n```javascript\n// Never include username or password in default SMTP-config\nconst defaultSQLiteUri = 'sqlite://path/to/db.file'\n// DB_URI = sqlite://[path/to/file]\nconst defaultSQLServerUri = 'mssql://username@db.test.com/InstanceName/DbName'\n// DB_URI = mssql://[username][:password]@[hostname]:[port]/[DbInstance]/[DbName]\nunpackSequelizeConfig('DB_URI', 'DB_PWD', defaultUri)\n```\n\nExamples of usage can be found int the node-api and node-web template projects.\n\n## Dotenv\n\nUse the [npm package **dotenv**][dotenv] to set environment variables during development.\nTake a look at the unit tests for example usage.\n\n[dotenv]: https://www.npmjs.com/package/dotenv\n\nThis code snippet loads env-vars with dotenv during development. In production it checks\nfor the availability of SERVICE_PUBLISH which is always set in a KTH Docker environment.\nIf it isn't found it will require localSettings.js which is the standard settings file\nin the KTH Ansible environment. There you can set env-vars by `process.env.ENV_VAR = 'value'`\n\n```javascript\n// Load .env file in development mode\nconst nodeEnv = process.env.NODE_ENV \u0026\u0026 process.env.NODE_ENV.toLowerCase()\nif (nodeEnv === 'development' || nodeEnv === 'dev' || !nodeEnv) {\n  require('dotenv').config()\n} else if (!process.env.SERVICE_PUBLISH) {\n  // This is an ANSIBLE machine which doesn't set env-vars atm\n  // so read localSettings.js which we now use to fake env-vars\n  // because it already exists in our Ansible setup.\n  require('../config/localSettings')\n}\n```\n\n## DEV NOTES\n\n- vi fimpar local-/prod-/ref-/devSettings.js\n  - läggs i commonSettings.js, serverSettings.js, browserSettings.js\n\n- fimpa full, safe och secure\n\nNär vi skapar nya settings-objektet\n\n- vi mergear commonSettings.js + serverSettings.js till servern\n- vi mergear commonSettings.js + browserSettings.js till browsern\n  - returneras på endpointen ./browserConfig som javascript\n    TODO: - lägg till script-tag i början av all layout-filer\n\n## Migrating from \u003c= 1.0.1\n\n- convert ...Settings.js files to:\n  - commonSettings.js -- shared by browser and server\n  - serverSettings.js -- server specific settings that should NEVER be sent to a browser\n  - browserSettings.js -- browser specific settings that are passed to browser\n\n- Search and replace:\n\n```\n  require('../**/configuration') =\u003e require('../**/configuration').server\n```\n\n- config.full =\u003e config\n- config.safe =\u003e config\n- [xxx].secure =\u003e [xxx]\n- server.full =\u003e server\n\n- What are these used for (kth-node-configuration)\n  - module.exports.getEnv = \\_env\n  - module.exports.getEnvString = \\_str\n  - module.exports.getEnvBool = \\_bool\n  - module.exports.getEnvInt = \\_int\n  - module.exports.devDefaults = \\_str\n\n- Update tests\n\n  config.full =\u003e config\n\n- change how we start server:\n\n  kth-node-server@1.x:\n\n  server.setConfig(config) =\u003e server.setConfig({ full: config })\n\n  kth-node-server@3.x:\n\n```javascript\nserver.start({\n  pfx: config.ssl.pfx,\n  passphrase: config.ssl.passphrase,\n  key: config.ssl.key,\n  ca: config.ssl.ca,\n  cert: config.ssl.cert,\n  port: config.port,\n  logger: log // Your logging service, could be console or kth-node-log\n})\n```\n\n- change in adldap.js (only if we use ldap)\n  - attributes: config.ldapClient.userattrs =\u003e attributes: config.ldap.userattrs\n  - config.ldapClient.filterReplaceHolder, kthid =\u003e config.ldap.filterReplaceHolder, kthid\n\n- change configuration.js (examples for node-web and node-api apps)\n\n_app.js_\nEdit app.js to look like this:\n\n```javascript\n'use strict'\n\nconst server = require('./server/server')\nrequire('./server/init')\n```\n\n_NODE-WEB:_\n\n```javascript\n'use strict'\nconst { generateConfig } = require('kth-node-configuration')\n\n// These settings are used by the server\nconst serverConfig = generateConfig([\n  require('../../../config/commonSettings'),\n  require('../../../config/serverSettings')\n])\n\nmodule.exports.server = serverConfig\n\n// These settings are passed to the browser\nconst browserConfig = generateConfig([\n  require('../../../config/commonSettings'),\n  require('../../../config/browserSettings')\n])\n\nmodule.exports.browser = browserConfig\n```\n\nIn adldap.js you need to change:\n\n- config.ldapClient =\u003e config.ldap\n\nAnd move any config settings from ldapClient object to ldap object.\n\n_NODE-API:_\n\n```javascript\n'use strict'\nconst { generateConfig } = require('kth-node-configuration')\n\n// These settings are used by the server\nconst serverConfig = generateConfig([require('../../../config/serverSettings')])\n\nmodule.exports.server = serverConfig\n```\n\n- add dependency to dotenv and have it load your .env-file on startup. server.js should start like this:\n\n```javascript\nconst server = require('kth-node-server')\n// Load .env file in development mode\nconst nodeEnv = process.env.NODE_ENV \u0026\u0026 process.env.NODE_ENV.toLowerCase()\nif (nodeEnv === 'development' || nodeEnv === 'dev' || !nodeEnv) {\n  require('dotenv').config()\n}\n// Now read the server config\nconst config = require('./init/configuration').server\n```\n\n- make sure configuration/index.js has the following export:\n\n```javascript\nmodule.exports = require('./configuration')\n```\n\n### The following steps only for frontend\n\n- Add endpoint .../browserConfig to staticFiles.js (earlier name: routing.js)\n\n```javascript\nconst paths = require('../routing/paths')\nconst browserConfig = require('../configuration').browser\nconst browserConfigHandler = require('kth-node-configuration').getHandler(browserConfig, paths)\n\n...\n\n// Expose browser configurations\nserver.use(config.proxyPrefixPath.uri + '/static/browserConfig', browserConfigHandler)\n```\n\n- add a line of code to load handlebars-helpers in server.js\n\n```javascript\n// Register handlebar helpers\nrequire('./views/helpers')\n```\n\n- Remove the handlebars helper if you have one\n\n```hbs\n  \u003c\u003cglobalSettingsForBrowserJS\u003e\u003e\n```\n\n- remove /buildConfig.js\n\n- remove npm script `\"buildConfig\"` and also remove call to it from `\"postinstall\"`-hook\n\n- Change config imports in js-files\n\n      \tvar config = require('config') =\u003e var config = { config: window.config, paths: window.paths }\n\n- include config in head, should look like this\n\n```hbs\n{{prefixScript '/static/js/vendor.js' 'head-scripts'}}\n{{prefixScript '/static/browserConfig' 'head-scripts'}}\n```\n\n## TODO\n\nTODO - add test for decodeUri.js\n\nTODO - add test for utils.js\n\nTODO - add test for unpackLDAPConfig.js\n\nTODO - add test for generateConfig.js\n\nTODO - add test for getHandler.js\n\n## DONE\n\nDONE - add test for unpackNodeApiConfig.js\n\nDONE - add test for unpackMongodbConfig.js\n\nDONE - add test for unpackRedisConfig.js\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkth%2Fkth-node-configuration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkth%2Fkth-node-configuration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkth%2Fkth-node-configuration/lists"}