In a real setting API server, is the data sent existing in arrays and objects within the file?


#1

Question

In a real setting API server, is the data sent existing in arrays and objects within the file?

Answer

No, we will not be storing data in the same files as we have been for our exercises.
The reason is that it will not be persisting data, since if we keep it as an object, or an array of values or even an array of objects in the file, every time we were to stop the server, all changes will disappear since the file will only have whatever we wrote in it but not any further changes introduced, ie.:

const express = require('express');
const app = express();

const PORT = process.env.PORT || 4001;

const buildingMaterials = {
  wood: ['plywood', '2x4s', 'cedar shingles'],
  metal: ['steel girders', 'wall studs', 'rebar'],
};

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

let’s say we have a CREATE method

...
app.post('/metals', (req,res,next)=>{...})
...

then we can receive new metals to add to the metal list in the buildingMaterials array, now, let’s imagine that we added three more through posts requests, the metals array should have six items now, but we closed the server and we restarted it, then if we call the get request for all metals we will be expecting six elements, but as I mentioned, data does not persists and we will see this as a response:

[
'steel girders',
 'wall studs', 
'rebar'
]

It is not sustainable, but that is why in real APIs we also implement storage technologies like SQL or NoSQL databases, yaml files, and cloud storage services for which we need to learn and implement other libraries or their documentation on how we communicate with them within our express server. Later, we will learn to use SQL databases and how to implement it with a library called Node-SQLite in our JS files.


FAQ: Code Challenges - Code Challenge