Node/Express: Trying to send static JSON file to API endpoint

Bennett Adams

I am working on a MEAN stack app, and I'm trying to create a couple of API endpoints that the Angular client can $http.get, with simple JSON files populated with dummy data.

Here's the orders.json file I'm trying to test it with:

[
  {
    "order_status":"Shipped",
    "order_qty":30
  },
  {
    "order_status":"Shipped",
    "order_qty":6
  }
]

For example, the api route to $http.get:

apiRouter.get('/:fileName', queries.getStaticJSONFileForDevelopment);

But when I try to use express's sendFile method with a local .json file, like orders.json:

queries.js:

exports.getStaticJSONFile = function(req, res) {

  var fileName = req.params.fileName;
  console.log('path: ' + path.normalize(__dirname + '/' + fileName));

  res.sendFile(path.normalize(__dirname + '/' + fileName), function(err) {
    if (err) return res.send({ reason:error.toString() });
  });
};

The console.log tells me I'm pointed at the correct path to the file, but Postman delivers this error:

TypeError: undefined is not a function
at Object.exports.getStaticJSONFile [as handle] (path/to/queries.js:260:7)
// queries.js:260:7 points to the 's' in 'sendFile' above

However, when I just send the json data by itself:

res.send([{"order_status":"Shipped","order_qty":30},{"order_status":"Shipped","order_qty":6}]);

...the endpoint renders the data as you would expect. Am I trying to get the sendFile method to do something it's not meant to do, or is there something I'm missing? Thanks very much for any advice you may have!

num8er

If You want to read json file and response with json so You can try this:

var jsonfile = require('jsonfile');

exports.getStaticJSONFile = function(req, res) {

  var fileName = req.params.fileName;
  var file = path.normalize(__dirname + '/' + fileName);
  console.log('path: ' + file);

  jsonfile.readFile(file, function(err, obj) {
    if(err) {
      res.json({status: 'error', reason: err.toString()});
      return;
    }

    res.json(obj);
  });
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Send JSON request to test Endpoint API in beego is failing with empty body

From Dev

Java upload a file to api endpoint

From Dev

Delphi send json + file

From Dev

Upload file using Guzzle 6 to API endpoint

From Dev

Retrofit 2.0, request GET to a .json file as endpoint

From Dev

Send an email from an API endpoint using Django Rest Framework and Angular

From Dev

Send an email from an API endpoint using Django Rest Framework and Angular

From Dev

Send a static file from server to client with socket

From Dev

Override send_static_file() in Flask

From Dev

OWIN send static file for multiple routes

From Dev

Can I send an excel file and JSON body with a description of file in same REST API Response

From Dev

trying to write nested JSON into File

From Dev

Teamwork api, how send file?

From Dev

How to send file in JSON on android?

From Dev

reading a web api blob into a string that I can send as part of a json object to the server and turn back into a file

From Dev

django file not found error: when javascript file inside static directory trying to access another file in the static directory?

From Dev

Trying to parse an json file with Swifty JSON

From Dev

How to Define Google Endpoints API File Download Message Endpoint

From Dev

How to pass file or directory path as a REST API parameter to Fistify endpoint

From Dev

How to Define Google Endpoints API File Download Message Endpoint

From Dev

Microsoft Graph API - file invite endpoint not working suddenly

From Dev

Change json format coming from api endpoint to use in component

From Dev

Trying to send a file with zmodem using lrzsz via java telnet server

From Dev

Glympse API Crash when trying to create/send a new Ticket

From Dev

getting http 400 when trying to send data to paypal api

From Dev

Trying to send access token from loopback to third party api

From Dev

Having trouble while trying to send URL via JSON in PHP

From Dev

I trying to send this json data form android to php server

From Dev

Trying to send a POST request and get json data but getting 400

Related Related

  1. 1

    Send JSON request to test Endpoint API in beego is failing with empty body

  2. 2

    Java upload a file to api endpoint

  3. 3

    Delphi send json + file

  4. 4

    Upload file using Guzzle 6 to API endpoint

  5. 5

    Retrofit 2.0, request GET to a .json file as endpoint

  6. 6

    Send an email from an API endpoint using Django Rest Framework and Angular

  7. 7

    Send an email from an API endpoint using Django Rest Framework and Angular

  8. 8

    Send a static file from server to client with socket

  9. 9

    Override send_static_file() in Flask

  10. 10

    OWIN send static file for multiple routes

  11. 11

    Can I send an excel file and JSON body with a description of file in same REST API Response

  12. 12

    trying to write nested JSON into File

  13. 13

    Teamwork api, how send file?

  14. 14

    How to send file in JSON on android?

  15. 15

    reading a web api blob into a string that I can send as part of a json object to the server and turn back into a file

  16. 16

    django file not found error: when javascript file inside static directory trying to access another file in the static directory?

  17. 17

    Trying to parse an json file with Swifty JSON

  18. 18

    How to Define Google Endpoints API File Download Message Endpoint

  19. 19

    How to pass file or directory path as a REST API parameter to Fistify endpoint

  20. 20

    How to Define Google Endpoints API File Download Message Endpoint

  21. 21

    Microsoft Graph API - file invite endpoint not working suddenly

  22. 22

    Change json format coming from api endpoint to use in component

  23. 23

    Trying to send a file with zmodem using lrzsz via java telnet server

  24. 24

    Glympse API Crash when trying to create/send a new Ticket

  25. 25

    getting http 400 when trying to send data to paypal api

  26. 26

    Trying to send access token from loopback to third party api

  27. 27

    Having trouble while trying to send URL via JSON in PHP

  28. 28

    I trying to send this json data form android to php server

  29. 29

    Trying to send a POST request and get json data but getting 400

HotTag

Archive