0

I want to read a json file from my folder and putting the result into my nodejs controller.

var request = require('request');
request('../global/language/gb.json', function (err, res, body) {

    if (err) {
        console.log(err);
    }

    if (!err && res.statusCode == 200) {
        var importedJSON = JSON.parse(body);
        console.log(importedJSON);
    }
})

I keep getting

Error: Invalid URI "../global/language/gb.json"
    at Request.init (G:\Dropbox\ThaiHome\Github\server\node_modules\request\request.js

Is that because it only likes a real http URL?

Or is there another way to read a json file from the harddisk?

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53
  • Possible duplicate of [Using Node.JS, how do I read a JSON object into (server) memory?](https://stackoverflow.com/questions/10011011/using-node-js-how-do-i-read-a-json-object-into-server-memory) – Maksim Nesterenko Jun 30 '17 at 20:55

1 Answers1

1

It's possible to just require your json file. I think you can't use request for your local files directly, you need some URI.

var result = require('../global/language/gb.json');

Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91