node js use json file as database code example
Example 1: nodejs json data serving
const userRoutes = (app, fs) => {
const dataPath = './data/users.json';
app.get('/users', (req, res) => {
fs.readFile(dataPath, 'utf8', (err, data) => {
if (err) {
throw err;
}
res.send(JSON.parse(data));
});
});
};
module.exports = userRoutes;
Example 2: nodejs json data serving
const userRoutes = (app, fs) => {
const readFile = (
callback,
returnJson = false,
filePath = dataPath,
encoding = 'utf8'
) => {
fs.readFile(filePath, encoding, (err, data) => {
if (err) {
throw err;
}
callback(returnJson ? JSON.parse(data) : data);
});
};
const writeFile = (
fileData,
callback,
filePath = dataPath,
encoding = 'utf8'
) => {
fs.writeFile(filePath, fileData, encoding, err => {
if (err) {
throw err;
}
callback();
});
};
app.get('/users', (req, res) => {
readFile(data => {
res.send(data);
}, true);
});
};
module.exports = userRoutes;