Create a simple Node.js API from JSON files

I have a folder of JSON files that I would like to use to create a simple API.

Here is a simplified version of my folder structure:

/clients.json

/clients/1/client.json

/clients/2/client.json

...

my /clients.json file looks like this:

 [ { "id": 1, "name": "Jon Parker" }, { "id": 2, "name": "Gareth Edwards" }, ... ] 

and my /clients/1/client.json file looks like this:

 [ { "date": "2014-09-12", "score": 40, ... }, { "date": "2015-02-27", "score": 75, ... }, { "date": "2015-05-10", "score": 75, ... }, { "date": "2016-08-27", "score": 60, ... } ] 

The identifier from clients.json refers to the folder that contains the related data.

I have many JSON files in the clients folder, and instead of loading them individually on the client side, I wanted to create an API using Node.js, which gives me more flexibility, that is ..

returns a list of client names and /clients identifiers

returns client data /clients/:id/details

and most importantly, the return of all customers with names and related details /clients/all/details

I started playing with json-server , however this requires your JSON to be an object, not an array, and I am stuck with the format of this JSON, unfortunately.

Appreciate any help!

+6
source share
5 answers

Use the built-in file system module to retrieve files from the file system.

See here

Here is an example.

 var fs = require('fs'); exports.getClientDetail = function (id) { var result; fs.readFile('/clients/' + id + '/client.json', function (err, data) { if (err) throw err; result.push(JSON.parse(data)); }); } exports.getAllClientsDetail = function () { // if the id is sequential, you can check if '/clients/' + id + '/client.json' exists for every i from 1 and up. If it does - push it to an array of objects. if for a certain i it does not exist, quit the scan and return the array of objects. } 
+4
source

You hardly get stuck, as you think.

You will have to wrap your arrays in an object. Then in the interface you just need to access the property of the array.

After all, JSON is an acronym for J ava s cript O bject N .. p>

EDIT: Alright, try something new ...

Perhaps, before using the code from the json server, do a little preprocessing. Assuming the clientJson variable is a file that you already read by pasting this code before using any code from the json server:

 clientJson = "{root:"+clientJson+"}"; 

This will transfer the file to the object with the first root property.

After that, it's pretty easy to get your array back:

 clientData = clientData.root; 
+2
source

You can require json directly as an object in node, something like this:

 app.get('/clients/:id/details', (req, resp) => { const id = req.params.id; const data = require(`./clients/${id}/client.json`); // or whatever path resp.send(data) }); 
+2
source

You can do this without any code if you upload your folder structure to a cloud service (e.g. Amazon S3 or Dropbox) and serve them from there. No code required.

+1
source

You should use Read streams from the FS module to send data to the client, catch possible errors and clear the memory after sending.

+1
source

Source: https://habr.com/ru/post/1013015/


All Articles