-1

Hey actually i am doing some project in Nodejs. I need a configuration file in order to store the data to file system but i do not know how to write a configuration file to store data to file. please help me with this. thanks in advance

Sushma
  • 21
  • 3
  • 9

3 Answers3

0

If you mean you need some constants and business logic/data file to read from, you can simply include the file in your script using the require module.

Ex: Your file name is test.json, then:

var test = require('test.json');

Further, you can use a CONSTANT in the file as 'test.CONSTANT'

Note: Please make sure you use module.exports wherever needed. Details are here

Community
  • 1
  • 1
Anshul Verma
  • 1,065
  • 1
  • 9
  • 26
0

Usually people use JSON to store configurations and stuff, since it is very javascripty.. You can simply make a JSON config file. In case you need to store some special data like SECRET URL, just use environment variables. FYI I found your question unclear. Does this answer your question.

const fs = require("fs");

// Example Config
let config = {
    DB: "mongodb://blahblah:idhdiw@jsjsdi",
    secret: "thisandthat",
    someScript: "blah.js"
};

// Write to file.
fs.writeFile('config.cfg', JSON.stringify(config), err => {
    if (err) throw err;
    console.log("[+] Config file saved!");
    // Retrieve
    let confData = JSON.parse(fs.readFileSync('config.cfg'));
    console.log(confData.secret);
});


// To save variables in environment variables
// Generally You will not set environment variables like this
// You will have access to setting environment variables incase
// you are using heroku or AWS from dash board. Incase of a machine            
// you can use ** export SOME_ENV_VAR="value" ** in your bash profile
process.env.IP = "10.10.10.10";
// Too risky to put else where.
process.env.API_KEY = "2ke9u82hde82h8";
// Get Data
console.log(process.env.IP);
console.log(process.env.API_KEY);
  • hello sir thanks for your replay actually i am doing a project on design tokens using node js . i have manually created a design tokens in .YAML file so my problem is whenever i need to store data the system should decide where to store data whether in db or file system. in order to do this i need a configuration fille. my question is how to write a configuration file in node js and what are the configuration variables are needed to store data in a file – Sushma Mar 17 '17 at 09:37
0

Sounds to me that you are looking for the following NPM module/library - dotenv. You simply require('dotenv').config(); which is probably best placed at the top (after use strict;) and create a text file which would read as an example:

url_prefix='mongodb://' url_ip='@localhost' port=':27017/' dbase='NameofDB'

Of course you can add anything you like to this file. Just remember it is a text file and should not contain spaces etc.

Though the default for the .env file is in the root of your project you can actually place it wherever you like, and simply put:

require('dotenv').config({path: '/custom/path/to/your/env/vars'});

(Above was taken from the dotenv documentation and it works as I use it in projects.)

To acquire any Global variable you would simply type: process.env.url_prefix

Obviously from there you can build the needed entry code to your DB from process.env statements such as: process.env.url_prefix+process.env.url_ip etc. OR ${process.env.url_prefix}${process.env.url_ip}

Using dotenv allows you to keep sane control over those process.env globals.

Be aware there is a gotcha! Be careful not to overwrite any of those globals in your code. As they will remain overwritten as long as your Node process is running.

twg
  • 1,075
  • 7
  • 11
  • hello sir thanks for your replay actually i am doing a project on design tokens using node js . i have manually created a design tokens in .YAML file so my problem is whenever i need to store data the system should decide where to store data whether in db or file system. in order to do this i need a configuration fille. my question is how to write a configuration file in node js and what are the configuration variables are needed to store data in a file – Sushma Mar 17 '17 at 09:53