Mukund Kumar
Mukund Kumar

Reputation: 23211

How to access nodejs config parameter in angular side?

i have nodejs config file(backend) in which some configuration data is defined:

module.exports = {
    product: {
        name: 'Event'
    },
    server: {

            host: '0.0.0.0',
            port: 8000
    },
    database: {
        host: '127.0.0.1',
        port: 27017,
        db: 'EventExchange',
        username: '',
        password: ''
    },
    key:{
        privateKey: 'dufediioeduedhn',
        tokenExpiry: 1*30*1000*60   //1 hour
    },
    admin:{
        "username" : "admssin",
        "password" : "adminss123",
        "scope"    : "adamssin"
    }
};

i have to access port and host parameter in angular(client side). how is it posssible ? Thanks!

Upvotes: 0

Views: 727

Answers (1)

Viral Pala
Viral Pala

Reputation: 69

You have to send them in a response (by sending JSON).

After that you can parse it using Json.parse.

For ex : At node side create a rest api

 app.get('/getConfig/', function (req, res, next) {
//NOW FETCH YOUR CONFIG
//PUT THE CONFIG IN JSON
var myConfig={port:database.port,host:database.hoat}
res.json(myConfig);
});

Now call this api from your AngularJs code by using $http

for ex:

$http.get("/getConfig").success(function(data))
{
console.log(data);
console.log(data.port);
console.log(data.host);
}

Hope this will help you !! Cheers :)

Upvotes: 4

Related Questions