Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the property file in node js?

I have found an article about properties file reader in node js here: https://www.npmjs.com/package/properties-reader

There is a module as 'properties-reader'. But, I'm unable to understand how to define the property file. Should it be a json?

like image 435
Prem Avatar asked Mar 05 '15 04:03

Prem


2 Answers

It's an ini format, as described here:

# contents of properties file
[main]
some.thing = foo

[blah]
some.thing = bar
like image 156
Abdullah Jibaly Avatar answered Oct 16 '22 21:10

Abdullah Jibaly


Its not a Json format, but in ini format.

Steps to set up properties file and to read it from your node module:

  1. make any properties file such as app.properties inside your project directory. The file may contain data like:

    \#comment(ignored)
    
    sever.port=3000
    
  2. Run the following command to install properties-reader locally:

    npm i properties-reader
    
  3. once done use the properties-reader like this:

    const PropertiesReader = require('properties-reader');
    const prop = PropertiesReader('path/to/app.properties');
    
    /*gets property from path/to/app.properties
    You can also export this function using module.exports*/
    getProperty = (pty) => {return prop.get(pty);}
    
    //call the getProperty method
    console.log(getProperty('server.port')); //3000
    

Easy as that!

like image 6
Ramsudharsan Manoharan Avatar answered Oct 16 '22 20:10

Ramsudharsan Manoharan