return json file node api 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
{
"1": {
"name": "king arthur",
"password": "password1",
"profession": "king",
"id": 1
},
"2": {
"name": "rob kendal",
"password": "password3",
"profession": "code fiddler",
"id": 2
},
"3": {
"name": "teresa may",
"password": "password2",
"profession": "brexit destroyer",
"id": 6
}
}