Adventures of a Hobbyist ~ Part Three
Tags: help, node, learning, opensource
Published: 2018-08-13 14:58:18

---
title: Adventures of a Hobbyist ~ Part Three
published: true
description: Thinking about conf files
tags: help, nodejs, learning, opensource
cover_image: https://static1.squarespace.com/static/5b3fbcd0697a98202418f839/t/5b435dc5f950b7059fb8eee6/1531141630672/Chalk-QuestionMark-header.jpg
series: Adventures of a Hobbyist
---

# Thinking about `conf` files

## What is this?

This is the third part of my 'learning to code' series, it's been slow progress but all progress is progress. If you're interested in reading about the project here are the first 2 parts of the series:

- [Preamble](https://dev.to/link2twenty/adventures-of-a-hobbyist--part-one-2e0n)
- [First week of Learning](https://dev.to/link2twenty/adventures-of-a-hobbyist--part-two-2g5a)

## What are conf files?

I have a background with Linux and server maintenance, it's quite common for programs to have `.conf` files to contain all of their settings. For web applications, I've made in the past, I've always hardcoded the database's location and then stored all other settings there. That approach doesn't really work when you want to make open source software as it needs to be easy for anyone to use it in their infrastructure.

To this end, I started thinking about using `.conf ` with node. It should be easy enough, I thought, we have `fs` built in and that's all we need. So I set about writing a little something to handle reading the conf file and getting the settings imported. This is what I ended up with.

####The conf file 
```javascript
"General": {
  "name": "ignis"
},
"MongoDB": {
  "host": "localhost",
  "port": "27017",
}
```

####The function to read it
```javascript
module.exports = {
  "loadConf": async () => {
    const fs = require('fs');
    const ConfFileLoc = "ignis.conf";

    async function getConfFile() {
      return new Promise((resolve) => {
        fs.readFile(ConfFileLoc, (err, data) => {
          if (err) throw err;
          resolve(JSON.parse(`{${data}}`));
        });
      });
    }

    let conf = await getConfFile();
    return conf;
  }
}
```

## So what's the problem?

I tried to access this data from another file, as I imagine it would be helpful to be able to read the `.conf` file from anywhere, but it would only tell me there was a pending promise. I worked out a way around this but it felt a bit hacky and I'm sure there's a simpler solution to this.

```javascript
const ch = require('./conf_helper');

(async () => {
  let conf = await ch.loadConf()
  console.log(conf)
})()
```

As you can see I've set the whole section as an `async` function but I'd have to have the entire file, apart from imports, in the `async` function which feels like a bad idea.

## I want to help.

If you want to help me out you can either leave a comment on this post or respond to my [GitHub issue](https://github.com/ignis-pwa/ideas/issues/14) about it. If I'm doing it a totally stupid way feel free to tell me, my aim is to learn how to do things properly.

## Side note.

You may have noticed I mentioned MongoDB in my examples above I'm not certain I want to use this yet. Historically, I've always used MySQL but MongoDB was suggested as a better solution so I'm looking into it. If you have any input for database discussion the [GitHub issue](https://github.com/ignis-pwa/ideas/issues/4) is the place to go.

## Fin.

Thank you so much for reading this far and coming with me on my journey of learning. If there is anything I can do to make these posts more interesting/engaging please let me know in the comments, I really appreciate any, and all, input I get and want to make this a good series for you, the read, as well as myself.

Thanks again,
Andrew.