Lucas Knäuper
Lucas Knäuper

Reputation: 35

understanding node.js-module 'config' and default.json & production.json & my actual problem with a "Configuration property"

https://github.com/node-config/node-config

Quote from github:

Install in your app diectory, and edit the default config file:

$ npm install config

$ mkdir config

$ vi config/default.json"

here the overview talks about how to install it and THEN creating the dir config. but after installing via 'npm install config' there already was a folder "node_modules/config" created. how could it be installed without that folder anyhow and why do I need to create a foulder AFTER the instalation??


more important question: these files do not exist:

and somehow my node.js server isn't taking my post request correctly and the debugger stops here:


M:...\Projekt22\node_modules\config\lib\config.js:182

throw new Error('Configuration property "' + property + '" is not defined'); ^

Error: Configuration property "sessionStorage.timeout" is not defined


So I wonder now, if there is an issue with my config.js, with the "config/default.json" or "config/production.json"... that don't exist...

Can anyone explain all this to me?


This are the lines, where my code stops working:

var expirationTime = config.get("sessionStorage.timeout")

var expiresAT = issueAt + (expirationTime * 1000)

And this is the whole module of my program, where the crash happens:

var userService = require("../user/UserService")
var jwt = require("jsonwebtoken") 
var config = require("config")
/* const logger = require("nodemon/lib/utils/log"); */


function createSessionToken(props, callback) {
    console.log("AuthenticationService: create Token");

    if (!props) {
        console.log("Error: have no json body")
        callback("JSON-Body missing", null, null)
        return
    }

    userService.findUserBy(props.userID, function (error, user) {

        if (user) {
            console.log("Found user, checking password...")

            user.comparePassword(props.password, function (err, isMatch) {

                if (err) {
                    console.log("Password is invalid")
                    callback(err, null);
                }
                else {
                    console.log("Password is correct. Create token.")

                    var issueAt = new Date().getTime()
                    var expirationTime = config.get("sessionStorage.timeout")
                    var expiresAT = issueAt + (expirationTime * 1000)
                    var privateKey = config.get("session.tokenKey")
                    let token = jwt.sign({ "user": user.userID }, privateKey, { expiresIn: expiresAT, algorithm: "HS256" })

                    console.log("Token created: " + token)

                    callback(null, token, user)
                }
            }
            )              
        }
        else {
            console.log("Session Services: Did not find user for user ID: " + props.userID)
            callback("Did not find user", null);
        }
    })
}

module.exports = {
    createSessionToken
}

Where is the problem?

Upvotes: 0

Views: 1278

Answers (2)

Nick Steele
Nick Steele

Reputation: 8027

You're just confusing the destination folder you're supposed to be putting your JSON. That's it! :)

The /config folder needs to be in your project root, not /node_modules/config (that is where node actually places the config npm package, which is why the directory is created when you did npm install config).

To fix things just do this...

  1. Create a file (in the root of your project) called /config/default.json
  2. Put this in it:
{
  "sessionStorage": {
    "timeout": 100
  }
}
  1. Then in your code...
// experationTime now equals "100" (or whatever your put in /config/default.json)! :)
var expirationTime = config.get("sessionStorage.timeout")

Presto!

The config npm can do all sorts of layering magic, so don't give up. it's one of the most powerful packages out there! :)

Upvotes: 0

Jimmy
Jimmy

Reputation: 1429

I realize that using the config word will confuse you, so we need to agree on this:

  1. I will use the word config to refer to the config package
  2. I will use setting to refer to the config folder you make yourself. (by using mkdir config)

Now come to your questions:

  1. npm install config registers the config package to your app. Without this, your app cannot use that config package, like when you use require('config'), it will report error

  2. The config package itself reads your configuration from the setting folder, that's the reason you have to mkdir config

  3. The setting folder above is not the same as in node_modules/config. The code of config package lives in node_modules. When you use require('config'), node will go and fetch the code from node_modules folder

  4. Those 2 json files are created by you, see the instruction:

vi config/default.json
vi config/production.json

vi is short for vim editor, it has a superpower where you don't need to create the file before editing it.

Upvotes: 0

Related Questions