title: How to push objects to an array in json-server with a database generated in JS?
body: <p>I have a json-server app running that generates the database on start dynamically with JS.
  The data I create is wrapped into a 'users' object as shown below:</p>
  <pre><code>{
      &quot;users&quot;: [
          {
              &quot;id&quot;: &quot;user_id_0&quot;,
              &quot;email&quot;: &quot;Andy_Cummerata@hotmail.com&quot;,
              &quot;first_name&quot;: &quot;Consuelo&quot;,
              &quot;last_name&quot;: &quot;Labadie&quot;,
              &quot;last_login_time&quot;: 1638643810,
              &quot;role&quot;: &quot;Manager&quot;
          },
          {
              &quot;id&quot;: &quot;user_id_1&quot;,
              &quot;email&quot;: &quot;Ricardo_Jacobs45@yahoo.com&quot;,
              &quot;first_name&quot;: &quot;Albina&quot;,
              &quot;last_name&quot;: &quot;Nienow&quot;,
              &quot;last_login_time&quot;: 1637479536,
              &quot;role&quot;: &quot;User&quot;
          }
      ],
      &quot;Page&quot;: 1,
      &quot;PageSize&quot;: 10,
      &quot;Total&quot;: 10
  }
  </code></pre>
  <p>I want to be able to push a new user into that array. Because I render my response to wrap it, if I just use the native POST, the request simply fails. So I want to write my own function for, but I can't figure out how to access and modify my created database?</p>
  <p><strong>How can I create a function to send data to my dynamically generated database?</strong></p>
  <p>Below are the rest of my files, in case it's of any use.</p>
  <p>File: db.js</p>
  <pre><code>var faker = require('faker');
  var utils = require('../utils.js');
  const users_created = 10
  
  module.exports = function () {
      const data = { users: [] }
  
      for (var i = 0; i &lt; users_created; i++) {
          data.users.push({
              id: &quot;user_id_&quot; + i,
              email: faker.internet.email(),
              first_name: faker.name.firstName(),
              last_name: faker.name.lastName(),
              last_login_time: utils.string_to_oom_epoch(faker.date.recent(days = 40).toISOString().replace('Z', '')),
              role: faker.helpers.randomize(['Admin', 'Manager', 'User'])
          })
      }
  
      return data;
  }
  </code></pre>
  <p>File: server.js</p>
  <pre><code>const jsonServer = require('json-server');
  const server = jsonServer.create()
  const router = jsonServer.router(require('./db.js')())
  const middlewares = jsonServer.defaults()
  
  server.use(middlewares)
  server.use(jsonServer.bodyParser)
  
  router.render = (req, res) =&gt; {
      if (req.url.startsWith('/generic_users_list')) {
              page = parseInt(req.query.page)
      pageSize = parseInt(req.query.pagesize)
      filter_email = req.query.email
  
      users = res.locals.data.users
      if (filter_email !== undefined) {
          filtered_users = []
          for (var i = 0; i &lt; users.length; i++)
              if (users[i].email.toLowerCase() == filter_email.toLowerCase())
                  filtered_users.push(users[i])
          users = filtered_users
      }
  
      if (!isNaN(pageSize)) {
          res.jsonp({
              users: users.slice(page * pageSize, (page + 1) * pageSize),
              Page: page,
              PageSize: pageSize,
              Total: users.length
          })
      } else {
          res.jsonp({
              users: users,
              Page: 1,
              PageSize: users.length,
              Total: users.length
          })
      }
      } else {
          res.jsonp(res.locals.data)
      }
  }
  
  server.post('/generic_users_list', (req, res) =&gt; {
      new_user = {
          id: req.body.id,
          email: req.body.email,
          first_name: req.body.first_name,
          last_name: req.body.last_name,
          last_login_time: req.body.last_login_time,
          role: req.body.role
      }
      // WHAT DO I HAVE TO DO HERE TO ADD 'new_user' TO MY DATABASE?
  })
  
  server.use(router)
  
  server.listen(port = 3000, () =&gt; {
      console.log(&quot;port&quot;)
      console.log('JSON Server is running')
      console.log('http://localhost:' + port)
  })
  </code></pre>