node serve json code example
Example 1: json server npm
npm install -g json-server
Example 2: nodejs json data serving
const userRoutes = (app, fs) => {
// variables
const dataPath = './data/users.json';
// READ
app.get('/users', (req, res) => {
fs.readFile(dataPath, 'utf8', (err, data) => {
if (err) {
throw err;
}
res.send(JSON.parse(data));
});
});
};
module.exports = userRoutes;