title: Express Middleware: ERROR: TypeError: Converting circular structure to JSON
body: <p>I'm using the following function as a middleware just to increment the id of a new object being added to my array:</p>
  
  <pre><code>let lions = []
  let id = 0
  
  const updateId = function(req, res, next) {
    if (!req.body.id) {
      id++;
      req.body.id = id + '';
    }
    next();
  };
  </code></pre>
  
  <p>When I post a new lion it will then hit this route:</p>
  
  <pre><code>app.post('/lions', updateId, (req, res) =&gt; {
    console.log('POST req', req.body)
    const lion = req.body;
    lions.push(lion)
  
    res.json(req)
  })
  </code></pre>
  
  <p>The POST works and the new lion is created, however I get the following error. Any ideas on how to fix it?</p>
  
  <blockquote>
    <p>[nodemon] starting <code>node server.js</code>
    NODE RUNNING on port: 3000
    GET lions: []
    ERROR: TypeError: Converting circular structure to JSON
        at JSON.stringify ()
        at stringify (/Users/leongaban/projects/tutorials/pluralsight/api-design-node/node_modules/express/lib/response.js:1119:12)</p>
  </blockquote>
  
  <h2>server.js</h2>
  
  <pre><code>// create a route middleware for POST /lions that will increment and
  // add an id to the incoming new lion object on req.body
  
  const express = require('express')
  const app = express()
  const bodyParser = require('body-parser')
  const port = 3000
  
  app.use(express.static('client'))
  app.use(bodyParser.urlencoded({ extended: true }))
  app.use(bodyParser.json())
  
  let lions = []
  let id = 0
  
  const updateId = function(req, res, next) {
    if (!req.body.id) {
      id++;
      req.body.id = id + '';
    }
    next();
  };
  
  app.param('id', (req, res, next, id) =&gt; {
    let lion = lions.filter((lion =&gt; lion.id === id))
  
    if (lion) {
      req.lion = lion;
      next();
    }
    else {
      console.log('NO LION')
      res.send()
    }
  })
  
  app.get('/lions', (req, res, next) =&gt; {
    console.log('GET lions:', lions)
    res.json(lions)
  })
  
  app.get('/lions/:id', (req, res) =&gt; {
    res.json(req || {})
  })
  
  app.post('/lions', updateId, (req, res) =&gt; {
    console.log('POST req', req.body)
    const lion = req.body;
    lions.push(lion)
  
    res.json(req)
  })
  
  app.put('/lions/:id', (req, res) =&gt; {
    const paramId = req.params.id
    const updated = req.body
  
    if (updated.id) delete updated.id
  
    const oldLion = lions.find((lion =&gt; lion.id === paramId))
  
    if (!oldLion) res.send()
  
    const newLion = Object.assign({ id: oldLion.id }, updated)
    lions = lions.filter(lion =&gt; lion.id !== paramId)
    lions.push(newLion)
  
    res.json(newLion)
  })
  
  app.delete('/lions/:id', (req, res) =&gt; {
    lions = lions.filter((lion =&gt; lion.id !== req.params.id))
  
    res.json(lions)
  })
  
  app.use((err, req, res, next) =&gt; {
    console.error('ERROR:', err)
  })
  
  app.listen(port, () =&gt; console.log(`NODE RUNNING on port: ${port}`))
  
  </code></pre>