CodeBlanks Logo Icon CodeBlanks

fs persistence

Save and load data as JSON with fs.

File persistence

Routes live in memory; they are lost when the server restarts. To make data persistent we write it to a file with the fs module. fs.writeFileSync(path, data) writes data synchronously; fs.readFileSync(path, 'utf8') reads the file as a string.

To store the user list you write a saveUsers(data) function: fs.writeFileSync(DB_PATH, JSON.stringify(data)). Here JSON.stringify turns the object into a string. You restore it with loadUsers(): JSON.parse(fs.readFileSync(DB_PATH, 'utf8')). JSON.parse turns the string back into an object. This way data persists in a file; it stays on disk even if the server crashes.

Save and load

In this lesson we add the saveUsers and loadUsers functions. saveUsers({users: ['Ayşe', 'Alex']}) writes the data to a file; loadUsers() reads the file and returns the object. Calling them in sequence makes the data persistent.

First we import the module with const fs = require('fs'). We pin the file path with const DB_PATH = '/tmp/nodejs-i20.json' and reuse it. saveUsers handles writing, loadUsers handles reading. Finally console.log('Loaded: ' + JSON.stringify(loadUsers())) verifies the data was restored. The output is the string form of the saved object.

Practice Add fs persistence

Import with const fs = require('fs'). Define const DB_PATH = '/tmp/nodejs-i20.json'. Write function saveUsers(data) { fs.writeFileSync(DB_PATH, JSON.stringify(data)); }. Write function loadUsers() { return JSON.parse(fs.readFileSync(DB_PATH, 'utf8')); }. Call saveUsers({users: ['Ayşe', 'Alex']}). Print console.log('Loaded: ' + JSON.stringify(loadUsers())).

Starter code

index.js
const express = require('express');
const app = express();
const routes = [];
app.get('/health', (req, res) => res.json({ok: true}));
routes.push({method: 'GET', path: '/health'});
console.log('Server ready');
app.get('/users', (req, res) => res.json([]));
routes.push({method: 'GET', path: '/users'});
app.post('/users', (req, res) => res.json({created: true}));
routes.push({method: 'POST', path: '/users'});
console.log('Routes: ' + routes.map(r => r.method + ' ' + r.path).join(', '));

// Import the fs module
// Write saveUsers (writeFileSync) and loadUsers (readFileSync)
// Save and load, print the result
// write here

Sample solution

index.js
const express = require('express');
const app = express();
const routes = [];
app.get('/health', (req, res) => res.json({ok: true}));
routes.push({method: 'GET', path: '/health'});
console.log('Server ready');
app.get('/users', (req, res) => res.json([]));
routes.push({method: 'GET', path: '/users'});
app.post('/users', (req, res) => res.json({created: true}));
routes.push({method: 'POST', path: '/users'});
console.log('Routes: ' + routes.map(r => r.method + ' ' + r.path).join(', '));
const fs = require('fs');
const DB_PATH = '/tmp/nodejs-i20.json';
function saveUsers(data) {
  fs.writeFileSync(DB_PATH, JSON.stringify(data));
}
function loadUsers() {
  return JSON.parse(fs.readFileSync(DB_PATH, 'utf8'));
}
saveUsers({users: ['Ayşe', 'Alex']});
console.log('Loaded: ' + JSON.stringify(loadUsers()));

Expected output

Sunucu hazır
Rotalar: GET /health, GET /users, POST /users
Yüklendi: {"users":["Ayşe","Alex"]}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each piece to its role

Term Meaning
writeFileSync Writes data to a file
readFileSync Reads the file as a string
JSON.stringify Turns an object into a string
JSON.parse Turns a string into an object

2 What does JSON.stringify({users: ['Ayşe']}) produce?

index.js
JSON.stringify({users: ['Ayşe']})
  • {users: ['Ayşe']}
  • users:Ayşe
  • [Ayşe]
  • {"users":["Ayşe"]} Correct answer

Explanation: JSON.stringify turns the object into the string {"users":["Ayşe"]}.

3 Complete the writeFileSync line

Question
fs.writeFileSync(DB_PATH, JSON.____(data));
Answer
fs.writeFileSync(DB_PATH, JSON.stringify(data));

Explanation: JSON.stringify turns the object into a string, writeFileSync writes it.

All lessons in Intermediate