0

I'm using NodeJS to build a sample page just for some learning. I'm trying to send a GET request to a file in the project's directory (using request module of node). The callback is just returning me an error and I can't figure out why this is happening. Here's the code:

request.get(__dirname+"/public/app/test.json",function(err,data){
    if(!err)
    {
        res.json(data);
        console.log("Test Data Sent!");
    }
    else
    {
        res.send(err);
        console.log("error occured! "+err);
    }
});

The console log says this: "Error: Invalid URI e:/directory/nodejs/node-project/public/app/test.json"

Here's my directory structure: (Bold means folders)

E:

----directory

--------nodejs

------------node-project

------------server.js

------------package.json

----------------public

--------------------app

--------------------test.json

Can someone point out the problem?

maham.shahid
  • 301
  • 4
  • 19
  • 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) – maham.shahid Sep 21 '18 at 22:32

1 Answers1

1

A GET is a http request made to a web server. You can't GET a local file directly.

A local file can be read with fs.readFile.

Matt
  • 68,711
  • 7
  • 155
  • 158