How to get a json file in express js and display in view


Question

I have a problem in getting a .json file in express and displaying in a view. Kindly share your examples.

1
15
9/29/2017 2:11:47 AM

var fs = require("fs"),
    json;

function readJsonFileSync(filepath, encoding){

    if (typeof (encoding) == 'undefined'){
        encoding = 'utf8';
    }
    var file = fs.readFileSync(filepath, encoding);
    return JSON.parse(file);
}

function getConfig(file){

    var filepath = __dirname + '/' + file;
    return readJsonFileSync(filepath);
}

//assume that config.json is in application root

json = getConfig('config.json');
28
8/7/2013 8:44:16 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon