return json file node api code example

Example 1: 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;

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
  }
}