{"id":2713,"date":"2018-11-09T14:02:06","date_gmt":"2018-11-09T12:02:06","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2713"},"modified":"2018-11-09T14:02:06","modified_gmt":"2018-11-09T12:02:06","slug":"node-js-domino-db-docker-9-global-configurations","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2713","title":{"rendered":"node.js, domino-db &#038; Docker (9): Global Configurations"},"content":{"rendered":"<p>The database configuration should not be changing during the different requests, that&#8217;s why it is a good idea to store the configuration in a central place of our express application. There are multiple ways of doing this, e.g. you can use <a href=\"https:\/\/expressjs.com\/en\/api.html#app.locals\" target=\"_blank\" rel=\"noopener\">app.locals<\/a> for this. Or you can use the <a href=\"https:\/\/expressjs.com\/en\/api.html#app.set\" target=\"_blank\" rel=\"noopener\">app.set<\/a> method.<\/p>\n<p>But the best option in my eyes is to have a global configuration file, because using the methods above requires access to the app object (see below how this works).<\/p>\n<h2>Using a global configuration file<\/h2>\n<p>1. Create a new file in the <em>\/app<\/em> folder named <em>config.js<\/em><\/p>\n<p>2. Add a global configuration object<\/p>\n<pre><code>const Config = {};\r\n\r\nmodule.exports = Config;<\/code><\/pre>\n<p>3. Add the configuration for our database<\/p>\n<pre><code>const Config = {\r\n    serverConfig: {\r\n        hostName: process.env.NODE_ENV === 'development' ? 'dev.example.com' : 'www.example.com', \/\/ Host name of your server\r\n        connection: {\r\n          port: '3002', \/\/ Proton port on your server\r\n        },\r\n      },\r\n      databaseConfig: {\r\n        filePath: 'node-demo.nsf', \/\/ The database file name\r\n      }\r\n};\r\n\r\nmodule.exports = Config;<\/code><\/pre>\n<p>The configuration above checks the environment variables. In development mode, the <em>dev.example.com<\/em> server is used, otherwise <em>www.example.com.<\/em><\/p>\n<p>4. Import the file in your code<\/p>\n<pre><code>const config = require('..\/config');<\/code><\/pre>\n<p>5. Use it by deconstructing the values<\/p>\n<pre><code>const { serverConfig, databaseConfig } = config;<\/code><\/pre>\n<p>This can be used everywhere in your application.<\/p>\n<h2>Using app.set<\/h2>\n<p>1. Open the file <em>app.js<\/em> in the <em>\/app<\/em> folder<\/p>\n<p>2. Add the following code before the <em>module.export<\/em> statement:<\/p>\n<pre><code>app.set('db', {\r\n  serverConfig: {\r\n    hostName: 'your.server.com', \/\/ Host name of your server\r\n    connection: {\r\n      port: '3002', \/\/ Proton port on your server\r\n    },\r\n  },\r\n  databaseConfig: {\r\n    filePath: 'node-demo.nsf', \/\/ The database file name\r\n  }\r\n});\r\n<\/code><\/pre>\n<p>3. If you want to have different configurations depending of development \/ productive environment, you can compute the property like this:<\/p>\n<pre><code>...\r\nhostName: app.get('env') === 'development' ? 'your.devserver.com' : 'your.server.com',\r\n...<\/code><\/pre>\n<p>4. To use the configuration in the router, just use the <em>app.get<\/em> method (provided by the request object) and deconstruct the values:<\/p>\n<pre><code>router.get('\/', (req, res) =&gt; {\r\n  const { serverConfig, databaseConfig } = req.app.get('db');\r\n  ...<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The database configuration should not be changing during the different requests, that&#8217;s why it is a good idea to store the configuration in a central place of our express application. There are multiple ways of doing this, e.g. you can &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2713\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[139,9,131],"tags":[138,140,133,132],"class_list":["post-2713","post","type-post","status-publish","format-standard","hentry","category-es6","category-javascript","category-node-js","tag-domino-db","tag-es6","tag-express","tag-node-js"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2713"}],"version-history":[{"count":3,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2713\/revisions"}],"predecessor-version":[{"id":2733,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2713\/revisions\/2733"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}